3-视频播放
- 1.Player
- 2.Initialization
- 3.Setting the Rendering Surface
- 4.Setting the Data Source
- 5.Playback Parameter Configuration
- 6.Playback Control
- 7.Callbacks
- 8.Destruction
1.Player
The core external interface of the player is IPLVMediaPlayer, and its implementation class is PLVMediaPlayer.
2.Initialization
You can directly create a player instance using the constructor, for example:
const player: PLVMediaPlayer = new PLVMediaPlayer();
3.Setting the Rendering Surface
The player renders using an XComponent of type surface, with the library name being plvplayer_xcomponent. You can set the rendering surface to the player using the setXComponent method:
XComponent({
id: `plv_media_player_video_xcomponent`,
type: "surface",
libraryname: "plvplayer_xcomponent"
}) {
}
.onLoad((xcomponent) => {
// 设置渲染画面
this.player.setXComponent(xcomponent)
})
4.Setting the Data Source
Set the data source by calling the interface setMediaResource():
/**
* 设置播放资源
*/
setMediaResource(mediaResource: PLVMediaResource): void
After calling this interface, playback will start automatically by default. You can also control automatic start through playback parameter configuration.
5.Playback Parameter Configuration
Set playback parameters by calling the interface setPlayerOption():
/**
* 设置播放参数
*/
setPlayerOption(options: PLVMediaPlayerOption[])
The PLVMediaPlayerOptionEnum class provides some commonly used playback parameters. You can directly reference its constants, for example:
// 开启精准seek的参数
PLVMediaPlayerOptionEnum.ENABLE_ACCURATE_SEEK.value("1")
For parameters set repeatedly, the new value will overwrite the old one. To clear a parameter, pass an empty string in the value field.
6.Playback Control
The player provides a series of playback control interfaces, for example:
/**
* 开始播放
*/
start(): void;
/**
* 暂停播放
*/
pause(): void;
/**
* 跳转播放进度到指定位置
* @param position 指定位置,单位:毫秒
*/
seek(position: number): void;
For more control operations, refer to IPLVMediaPlayer and its parent interface IPLVMediaPlayerControl.
7.Callbacks
Player status and event callbacks can be monitored through the callback registration center, including:
- IPLVMediaPlayerBusinessListenerRegistry: Player business callback registration center
- IPLVMediaPlayerEventListenerRegistry: Player event callback registration center
- IPLVMediaPlayerStateListenerRegistry: Player state callback registration center
For example, to monitor the play/pause state, you can do so as follows:
const playingState: MutableState<PLVMediaPlayerPlayingState> = player.getStateListenerRegistry().playingState;
const observer = playingState.observe((state: PLVMediaPlayerPlayingState) => {
// 处理逻辑
const isPlaying = state === PLVMediaPlayerPlayingState.PLAYING;
});
8.Destruction
When the player is no longer needed after playback ends, it should be destroyed:
player.destroy();
