3-视频播放
1. Player
The core external interface of the player is IPLVMediaPlayer. The external implementation classes for this interface are PLVMediaPlayer and PLVVideoView, with the following differences:
- PLVMediaPlayer: The core player class, providing full player functionality but without a rendering interface.
- PLVVideoView: Wraps PLVMediaPlayer, adds a default rendering interface, and can be used as a View.
2. Initialization
You can directly create a player instance using the constructor, for example:
new PLVMediaPlayer();
new PLVVideoView(context);
Additionally, since PLVVideoView inherits from FrameLayout, you can also declare PLVVideoView directly in the layout file and then obtain the instance in code via findViewById().
3. Setting the Data Source
Set the data source by calling the interface setMediaResource():
/**
* 设置播放资源
*/
fun setMediaResource(mediaResource: PLVMediaResource)
After calling this interface, playback will start automatically by default. You can also configure playback parameters to prevent automatic start.
4. Playback Parameter Configuration
Configure playback parameters by calling the interface setPlayerOption():
/**
* 设置播放参数
*/
fun setPlayerOption(options: List<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 setting will overwrite the old one. To clear a parameter, pass an empty string in the value field.
5. Playback Control
The player provides a series of playback control interfaces, for example:
/**
* 开始播放
*/
fun start()
/**
* 暂停播放
*/
fun pause()
/**
* 跳转播放进度到指定位置
* @param position 指定位置,单位:毫秒
*/
fun seek(position: Long)
For more control operations, refer to IPLVMediaPlayer and its parent interface IPLVMediaPlayerControl.
6. 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:
State<PLVMediaPlayerPlayingState> playingState = mediaPlayer.getStateListenerRegistry().getPlayingState();
MutableObserver<PLVMediaPlayerPlayingState> observer = playingState.observe(new Function1<PLVMediaPlayerPlayingState, Unit>() {
@Override
public Unit invoke(PLVMediaPlayerPlayingState playingState) {
// 处理逻辑
return null;
}
});
// 在不需要继续监听时,可以取消监听
observer.dispose();
7. Destruction
When the player is no longer needed after playback ends, it should be destroyed:
mediaPlayer.destroy();
