lwlplayer
简介
一款OpenHarmony环境下可用的基于FFmpeg和OHAudio的轻量级视频播放器。
- 支持直播、点播 (直播支持hlv、rtmp、rtsp协议)
- 支持播放、停止、倍速、释放(倍速最高支持到2倍)
- 支持快进、后退
- 支持多XComponent渲染
- 支持设置音量
- 支持FFmpeg软解码
引入依赖
ohpm install @lyzone/lwlplayer接口能力
| 接口 | 参数 | 返回值 | 说明 |
|---|---|---|---|
| initLwlPlayerXComponentController | isMain: boolean, position: number | XComponentController | 获取XComponentController对象 |
| createPlayer | type: LwlPlayerType | number | 创建播放器实例 |
| setPlayerCallback | ptr: number, callback: object | void | 设置播放器状态回调 |
| setFrameSize | width: number, height: number | void | 设置视频帧宽高 |
| playStream | ptr: number, path: string | number | 开启直播流 |
| switchStream | ptr: number, path: string | void | 切换直播地址 |
| stopStream | ptr: number | void | 关闭直播流 |
| setStreamProgress | ptr: number, time: number | void | 设置直播进度条 |
| isMute | ptr: number, type: LwlPlayerType, mute: boolean | void | 开/关声音 |
| setRate | ptr: number, type: LwlPlayerType, rate: LwlVideoRate | void | 视频倍速 |
| play | ptr: number, path: string, seek: number | void | 开始播放(http&本地文件) |
| setProgress | ptr: number, time: number | void | 设置进度条(http&本地文件) |
| seek | ptr: number, path: string, start: number, end: number | void | 快进 |
| stop | ptr: number | void | 停止播放 |
| destroy | ptr: number, type: LwlPlayerType | void | 销毁播放器 |
场景示例
下面提供两个场景示例以供参考。
场景一:单XComponent组件渲染播放rtmp直播流视频。
ts/** * 单XComponent渲染直播流 */ @Entry @Component struct SingleVideo { // 创建播放器 private player: LwlPlayer = new LwlPlayer() // XComponent控制器 private mainController: XComponentController | null = null // 播放器索引 @State ptr: number = -1 // 设置回调 private callback: LwlStreamPlayerCallback = { onStatusChange: (status: number, msg: string): void => { console.log("playCallback: " + status + ", msg: " + msg); }, frameSize: (width: number, height: number): void => { console.log("frameSize:" + width + ", height: " + height); // 设置帧宽高 this.player.setFrameSize(width, height); }, backProgress: (progress: number): void => { console.log("backProgress progress:" + progress) } } aboutToAppear() { // 创建播放器 this.mainController = this.player.initLwlPlayerXComponentController(true) // 初始化直播流播放器 this.ptr = this.player.createPlayer(LwlPlayerType.STREAM) // 设置回调 this.player.setPlayerCallback(this.ptr, this.callback) } aboutToDisappear(): void { // 销毁播放器 this.player.destroy(this.ptr, LwlPlayerType.STREAM) } build() { Column({ space: 10 }) { XComponent({ id: "XComponent1", type: XComponentType.SURFACE, controller: this.mainController }) .width('100%') .aspectRatio(16 / 9) Column({ space: 10 }) { Button("开始播放直播").onClick(() => { // 使用你自己的播放地址即可 let url: string = "" this.player.playStream(this.ptr, url) this.player.setStreamProgress(this.ptr, 0) }) Button("停止播放直播").onClick(() => { this.player.stopStream(this.ptr) }) Button("切换直播流").onClick(() => { // 使用你自己的播放地址即可 let url: string = "" this.player.switchStream(this.ptr, url) }) Row() { Button("开启声音").onClick(() => { this.player.isMute(this.ptr, LwlPlayerType.STREAM, true) }) Button("关闭声音").onClick(() => { this.player.isMute(this.ptr, LwlPlayerType.STREAM, false) }) } Button("1倍速").onClick(() => { this.player.setRate(this.ptr, LwlPlayerType.STREAM, LwlVideoRate.DEFAULT) }) Button("2倍速").onClick(() => { this.player.setRate(this.ptr, LwlPlayerType.STREAM, LwlVideoRate.DOUBLE) }) } }.width('100%') } }场景二:多XComponent组件(三个)渲染播放rtmp直播流视频。
ts/** * 多XComponent渲染直播流 */ @Entry @Component struct MultiVideo { // 创建播放器 private player: LwlPlayer = new LwlPlayer() // 主XComponent控制器 private mainController: XComponentController | null = null // 副XComponent控制器 private secondController: XComponentController | null = null // 额外XComponent控制器 private thirdController: XComponentController | null = null // 播放器索引 @State ptr: number = -1 // 设置回调 private callback: LwlStreamPlayerCallback = { onStatusChange: (status: number, msg: string): void => { console.log("playCallback: " + status + ", msg: " + msg); }, frameSize: (width: number, height: number): void => { console.log("frameSize:" + width + ", height: " + height); this.player.setFrameSize(width, height); }, backProgress: (progress: number): void => { console.log("backProgress progress:" + progress) } } aboutToAppear() { // 创建播放器 this.mainController = this.player.initLwlPlayerXComponentController(true, 1) this.secondController = this.player.initLwlPlayerXComponentController(false, 5) this.thirdController = this.player.initLwlPlayerXComponentController(false, 6) // 初始化播放器 this.ptr = this.player.createPlayer(LwlPlayerType.STREAM) // 设置回调 this.player.setPlayerCallback(this.ptr, this.callback) } aboutToDisappear(): void { this.player.destroy(this.ptr, LwlPlayerType.STREAM) } build() { Column({ space: 10 }) { Column() { XComponent({ id: "XComponent1", type: XComponentType.SURFACE, controller: this.mainController }) .width('100%') .aspectRatio(16 / 9) Row() { XComponent({ id: "XComponent2", type: XComponentType.SURFACE, controller: this.secondController }) .width('50%') .aspectRatio(16 / 9) XComponent({ id: "XComponent3", type: XComponentType.SURFACE, controller: this.thirdController }) .width('50%') .aspectRatio(16 / 9) } } Column({ space: 10 }) { Button("开始播放直播").onClick(() => { // 使用你自己的播放地址即可 let url: string = "" this.player.playStream(this.ptr, url) this.player.setStreamProgress(this.ptr, 0) }) Button("停止播放直播").onClick(() => { this.player.stopStream(this.ptr) }) Button("切换直播流").onClick(() => { // 使用你自己的播放地址即可 let url: string = "" this.player.switchStream(this.ptr, url) }) Row() { Button("开启声音").onClick(() => { this.player.isMute(this.ptr, LwlPlayerType.STREAM, true) }) Button("关闭声音").onClick(() => { this.player.isMute(this.ptr, LwlPlayerType.STREAM, false) }) } Button("2倍速").onClick(() => { this.player.setRate(this.ptr, LwlPlayerType.STREAM, LwlVideoRate.DOUBLE) }) } }.width('100%') } }
贡献代码
使用过程中发现任何问题都可以提 Issue,也欢迎您发 PR
QQ交流群:1041332978
