Polyv Help Center

Help Center

Quick Start

Updated: 2025-10-10 12:02:04

This guide will help you quickly get started with the Polyv WeChat Mini Program Live Player SDK, demonstrating its basic usage through a simple example.

Basic Usage

1. Import the SDK

import PolyvLive from '@polyv/wx-live-player-core'

2. Create a Player Instance

The player type is PolyvLive, and a configuration object playerConfig needs to be passed in.

const player = new PolyvLive(playerConfig)

3. Listen to Player Events

The player provides a rich set of events. You can listen to these events using the on method.

import { PlayerEventsType } from '@polyv/wx-live-player-core'

// 监听播放器就绪事件
player.on(PlayerEventsType.READY, () => {
  console.log('播放器已就绪')
})

// 监听错误事件
player.on(PlayerEventsType.ERROR, (error) => {
  console.error('播放器错误:', error)
})

4. Initialize the Player

// 初始化播放器
player.setUp()

Complete Example

<view>
  <live-player
    wx:if="{{isPlayerShow}}"
    id="live-player"
    src="{{src}}"
  >
  <live-player>
</view>

import PolyvLive, { PlayerEventsType } from '@polyv/wx-live-player-core'

Page({
  data: {
    playerConfig: {
      auth: {
        appId,
        sign,
        timeStamp
      },
      uid,
      cid,
      statistics: {
        param1: 'xxx',
        param2: 'xxx'
      },
    },
    isPlayerShow: false
  },

  onLoad() {
    this.initPlayer()
  },

  initPlayer() {
    const player = new PolyvLive(this.data.playerConfig)

    player.on(PlayerEventsType.READY, () => {
      // 播放器就绪后显示播放器,避免黑屏时间过长
      this.setData({
        isPlayerShow: true
      })
    })

    player.on(PlayerEventsType.UPDATE_MEDIA_INFO, (mediaInfo) => {
      const { type, src } = mediaInfo
      if (type === 'live') {
        this.setData({
          src
        })
      }
    })

    player.on(PlayerEventsType.ERROR, (error) => {
      console.error('播放器错误:', error)
    })

    player.setUp()

    this.player = player
  },

  onUnload() {
    // 页面卸载时释放播放器资源
    if (this.player) {
      this.player.destroy()
    }
  }
})

More Features

Notes

  1. Ensure that installation and configuration are completed before using the SDK.
  2. Bind player event listeners before setUp().
  3. The player instance consumes system resources. Remember to call the destroy() method to release resources when no longer needed.
  4. Handle player error events appropriately and provide proper user feedback.
联系客服,在线咨询
在线咨询