快速集成
Preparation
Prepare a Polyv account. In the live streaming backend, navigate to Settings -> Developer Settings to obtain the appId (App ID), appSecret (App Secret), and userId (Account ID) from your account's live streaming system. From Live -> Live List, obtain the channelId (Channel ID). If logging into a playback, you additionally need the vid (Playback Video ID) from the corresponding playback list for that channel.
Environment Requirements
| Name | Requirement |
|---|---|
| Hbuilderx | >=4.85 |
Usage
Method 1: Using PLVLiveScenesPluginHelper (Recommended)
Download PLVLiveScenesPluginHelper and place it in your project directory. If you are developing with VSCode, you can also include the .d.ts file from the directory for better type hints.
Using PLVLiveScenesPluginHelper eliminates the need to worry about how to import or the order of importing native plugins.
import { PLVLiveScenesPluginHelper } from "项目目录/plv-live-scense-plugin-helper";
Instantiating the Plugin
Create a plugin instance. All subsequent operations are performed through this instance.
const plugin = PLVLiveScenesPluginHelper(options);
PLVLiveScenesPluginHelper(options)
Parameters options:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
type |
PluginType | No | Plugin type, defaults to LiveScenesPlugin |
env |
String | No | Plugin runtime environment, defaults to 'development'. Optional values include 'development' and 'production'. This value determines log output behavior. |
enum PluginType {
/** 纯观看插件 */
LiveScenesPlugin = "PLV-LiveScenesPlugin",
/** 观看 + 开播复合插件 */
LiveScenesHostPlugin = "PLV-LiveUniPlugin",
}
initService(options)
Initialize the plugin service. This method must be called before using other features.
Parameters options:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
viewerId |
String | Yes | Viewer ID, used to uniquely identify the user, e.g., user's phone number. |
viewerName |
String | Yes | Viewer nickname. |
viewerAvatar |
String | Yes | Viewer avatar URL. |
code |
String | No | Marquee configuration code (optional). |
Example:
await plugin.initService({
viewerId: "user001",
viewerName: "UniappDemo用户",
viewerAvatar: "https://example.com/avatar.jpg",
});
setUserInfo(options)
To prevent the app from being sniffed and reverse-engineered to obtain developer configuration information, it is strongly recommended that developers design their own encryption method to encrypt userId, AppID, and AppSecret on the server side. Obtain the encrypted string via an HTTPS interface, decrypt it, and then pass the parameters to the SDK.
Set user account information. Call this after initService and before entering a live room or playback.
Parameters options:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
appId |
String | Yes | The App ID applied for on the Polyv platform. |
userId |
String | Yes | The User ID applied for on the Polyv platform. |
appSecret |
String | Yes | The App Secret applied for on the Polyv platform. |
Example:
await plugin.setUserInfo({
appId: "YOUR_APP_ID",
userId: "YOUR_USER_ID",
appSecret: "YOUR_APP_SECRET",
});
loginLiveRoom(options)
Log in and enter a live room.
Parameters options:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
sceneType |
Number | Yes | Scene type: 1 for Cloud Classroom, 2 for Live Commerce. |
channelId |
String | Yes | The unique identifier ID of the channel. |
Example:
await plugin.loginLiveRoom({
sceneType: 1, // 云课堂场景
channelId: "YOUR_CHANNEL_ID",
});
loginPlaybackRoom(options)
Log in and enter a playback room.
Parameters options:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
sceneType |
Number | Yes | Scene type: 1 for Cloud Classroom, 2 for Live Commerce. |
channelId |
String | Yes | The unique identifier ID of the channel. |
videoId |
String | Yes | The Vid of the video. |
vodType |
Number | Yes | Playback type: 0 for single video playback, 1 for playback list. |
Example:
await plugin.loginPlaybackRoom({
sceneType: 1,
channelId: "YOUR_CHANNEL_ID",
videoId: "YOUR_VIDEO_ID",
vodType: 0, // 单个视频回放
});
logoutRoomMessage(callback)
Register a callback function that is triggered when a user exits the room (currently only supported on iOS).
Parameters callback:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
res |
Function | Yes | The callback function executed after exiting the room. |
Example:
plugin.logoutRoomMessage((res) => {
console.log("已退出房间", res);
});
The broadcasting feature requires integrating the full-featured version of the plugin and setting the type to LiveScenesHostPlugin.
import {
PLVLiveScenesPluginHelper,
PluginType,
} from "../../tools/plv-live-scense-plugin-helper.mjs";
const plugin = PLVLiveScenesPluginHelper({
type: PluginType.LiveScenesHostPlugin,
});
loginStreamer
Log in from the host side to start broadcasting.
Parameters options:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
channelId |
String | Yes | The unique identifier ID of the channel. |
password |
String | Yes | The channel login password. |
nickname |
Number | Yes | The host's name. |
Example:
plugin.loginStreamer({
channelId: "YOUR_CHANNEL_ID",
password: "YOUR_CHANNEL_PASSWORD",
nickname: "YOUR_NAME",
});
setScreenShareGroup
Set up screen sharing (required only for iOS).
Parameters options:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
appGroup |
String | Yes | The screen sharing group identifier. |
Example:
plugin.setScreenShareGroup(YOUR_APP_GROUP);
Complete Usage Flow
A typical usage flow is as follows:
- Import and instantiate the plugin.
- Call
initServiceto initialize the service. - Call
setUserInfoto set user information. - Call
loginLiveRoomorloginPlaybackRoomas needed to enter a live stream or playback.
import { PLVLiveScenesPluginHelper } from "./tools/plv-live-scense-plugin-helper.js";
const plugin = PLVLiveScenesPluginHelper();
async function start() {
try {
// 1. 初始化服务
await plugin.initService({
viewerId: "user001",
viewerName: "Demo用户",
viewerAvatar: "https://example.com/avatar.jpg",
});
// 2. 设置用户信息
await plugin.setUserInfo({
appId: "YOUR_APP_ID",
userId: "YOUR_USER_ID",
appSecret: "YOUR_APP_SECRET",
});
// 3. 登录直播间
await plugin.loginLiveRoom({
sceneType: 1,
channelId: "YOUR_CHANNEL_ID",
});
console.log("登录成功");
} catch (error) {
console.error("发生错误:", error);
}
}
start();
Method 2: Manually Importing Native Plugins
Manually import native plugins.
const playModule = uni.requireNativePlugin("PLV-LiveScenesPlugin-PlayModule");
const configModule = uni.requireNativePlugin(
"PLV-LiveScenesPlugin-ConfigModule"
);
For the full-featured version:
var playModule = uni.requireNativePlugin("PLV-LiveUniPlugin-WatchPlayModule")
var configModule = uni.requireNativePlugin("PLV-LiveUniPlugin-WatchConfigModule")
Viewer Configuration Module - ConfigModule
ConfigModule encapsulates account information, user information, and SDK configuration functions.
To play Polyv videos, developers must first register an account on the Polyv official website. After logging in, navigate to Cloud Live -> Development Settings to obtain userId, AppID, and AppSecret. Encrypt these to obtain an encrypted string, place it on your own server, then retrieve the encrypted string from the mobile app via the network, decrypt it locally in the app, and set it using the setConfig method. Unless otherwise specified, configuration module information must be configured before entering the live room.
1. setViewerInfo
Configure user information for the live room. (User attributes in the live room cannot be modified after initialization.)
Method: setViewerInfo()
Example:
configModule.setViewerInfo({ viewerId:"", viewerName: "", viewerAvatar: "", },
(result) => { })
Parameters:
| Name | Type | Description |
|---|---|---|
| params.viewerId | String | Corresponds to the User ID in the viewing logs. |
| params.viewerName | String | Corresponds to the user nickname in the viewing logs. |
| params.viewerAvatar | String | The avatar of the viewing user. |
| callback | function {"isSuccess": 0, @"errMsg": ""} |
Callback for the execution result, returning success status and description. |
2. setConfig
Set account attributes for multiple scenarios. To prevent the app from being sniffed and reverse-engineered to obtain developer configuration information, it is strongly recommended that developers design their own encryption method to encrypt userId, AppID, and AppSecret on the server side. Obtain the encrypted string via an HTTPS interface, decrypt it, and then pass the parameters to the SDK.
Method: setConfig()
Example:
configModule.setConfig({
appId: "",
userId: "",
appSecret: ""
}, (result) => {
});
Parameters:
| Name | Type | Description |
|---|---|---|
params.appId (必需) |
String | Polyv Cloud Live App ID (available in the Cloud Live backend plugin). |
params.userId(必需) |
String | Polyv Cloud Live Account ID. |
params.appSecret(必需) |
String | Polyv Cloud Live App Secret. |
| callback | function {"isSuccess": 0, @"errMsg": ""} |
Callback for the execution result, returning success status and description. |
3. setMarqueeConfig
Set configuration properties for a custom marquee.
Method: setMarqueeConfig()
Example:
configModule.setMarqueeConfig({
code: "",
}, (result) => {
})
Parameters:
| Name | Type | Description |
|---|---|---|
| params.code | String | Marquee code parameter configuration. |
| callback | function {"isSuccess": 0, @"errMsg": ""} |
Callback for the execution result, returning success status and description. |
Viewer Playback Module - PlayConfig
playModule encapsulates the live and playback functions for Cloud Classroom and Live Commerce.
4. showFullScreenButtonOnIPad
Whether to display the full-screen button on iPad (only applicable in iPad Cloud Classroom scenarios). Must be called before entering the room.
Method: showFullScreenButtonOnIPad()
Example:
playModule.showFullScreenButtonOnIPad({show:true});
Parameters:
| Name | Type | Description |
|---|---|---|
show (必需) |
Bool(true to show full-screen button, false to hide) |
5. loginLiveRoom
Live login (for Cloud Classroom and Live Commerce scenarios).
Method: loginLiveRoom()
Example:
// 直播
playModule.loginLiveRoom(
1,
{
channelId: "",
liveParam4: "",
liveParam5: "",
},
(result) => {}
);
Parameters:
| Name | Type | Description |
|---|---|---|
sceneType (必需) |
Number(1 for Cloud Classroom, 2 for Live Commerce) | Live room type. |
params.channelId(必需) |
String | The channel ID for the live stream. |
params.liveParam4(非必需) |
||
params.liveParam5(非必需) |
||
| callback | function {"isSuccess": 0, @"errMsg": ""} |
Callback for the execution result, returning success status and description. |
6. loginPlaybackRoom
Login to a playback live room (for Cloud Classroom and Live Commerce playback).
Method: loginPlaybackRoom()
Example:
playModule.loginPlaybackRoom(
0,
{
channlId: "",
liveParam4: "",
liveParam5: "",
videoId: "",
vodType: 0,
},
(result) => {}
);
Parameters:
| Name | Type | Description |
|---|---|---|
sceneType (必需) |
Number(1 for Cloud Classroom, 2 for Live Commerce) | Live room type. |
params.channelId(必需) |
String | The channel ID for the live stream. |
params.liveParam4(非必需) |
||
params.liveParam5(非必需) |
||
params.videoId(必需) |
String | The playback video ID. |
params.vodType(必需) |
Number 0 for playback video, 1 for playback list | |
| callback | function {"isSuccess": 0, @"errMsg": ""} |
Callback for the execution result, returning success status and description. |
7. exitRoomCallback (iOS only)
Callback for exiting the live room.
Method: setExitRoomCallback()
Example:
playModule.setExitRoomCallback((res) => {
Logger.info("退出房间成功");
cb && cb(res);
});
Host Configuration Module
var liveModule = uni.requireNativePlugin("PLV-LiveUniPlugin-StreamerLiveModule")
var configModule = uni.requireNativePlugin("PLV-LiveUniPlugin-StreamerConfigModule")
1. setAppGroup [iOS Only]
App Group for screen sharing account configuration.
Method: setAppGroup()
Example:
configModule.setAppGroup({
appGroup:""
}, (result) => {
})
Parameters:
| Name | Type | Description |
|---|---|---|
| params.appGroup | String | The App Group configured for the account. |
| callback | function {"isSuccess": 0, @"errMsg": ""} |
Callback for the execution result, returning success status and description. |
Host Live Module - LiveModule
Login to start broadcasting and push stream.
Method: loginStreamer()
Example:
playModule.loginStreamer({
channelId: "",
password: "",
nickname: ""
}, (result) => {
});
Parameters:
| Name | Type | Description |
|---|---|---|
channelId (Required) |
String | Login channel ID. |
password (Required) |
String | Login password. |
nickname (Optional) |
String | Nickname. |
| callback | function {"isSuccess": 0, @"errMsg": ""} |
Callback for the execution result, returning success status and description. |
