# LyzoneMedia **Repository Path**: monad_c/lyzone-media ## Basic Information - **Project Name**: LyzoneMedia - **Description**: 一款OpenHarmony环境下可用的基于FFmpeg和OHAudio的轻量级视频播放器。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-10-21 - **Last Updated**: 2025-10-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README --- --- # 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 | 销毁播放器 | ## 场景示例 - 使用 lwlplayer 播放视频,实例单XComponent组件和多XComponent播放视频。 - 已有上架应用大谷云采用该播放器,参考:https://appgallery.huawei.com/app/detail?id=com.dgiot.myhome&channelId=SHARE&source=appshare ```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%') } } ``` ```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%') } } ``` ​ 更多使用场景和示例,例如画中画,不同播放器间切换,绑定进度条等,可以参考本库代码仓的 entry 示例工程:https://gitee.com/monad_c/lyzone-media.git 使用过程中存在任何相关问题欢迎各位开发者提Issue和PR,或者加群反馈(Q群:1041332978),欢迎大家一起共建完善该库。