Skip to content

lwlplayer

简介

一款OpenHarmony环境下可用的基于FFmpeg和OHAudio的轻量级视频播放器。

  • 支持直播、点播 (直播支持hlv、rtmp、rtsp协议)
  • 支持播放、停止、倍速、释放(倍速最高支持到2倍)
  • 支持快进、后退
  • 支持多XComponent渲染
  • 支持设置音量
  • 支持FFmpeg软解码

引入依赖

ohpm install @lyzone/lwlplayer

接口能力

接口参数返回值说明
initLwlPlayerXComponentControllerisMain: boolean, position: numberXComponentController获取XComponentController对象
createPlayertype: LwlPlayerTypenumber创建播放器实例
setPlayerCallbackptr: number, callback: objectvoid设置播放器状态回调
setFrameSizewidth: number, height: numbervoid设置视频帧宽高
playStreamptr: number, path: stringnumber开启直播流
switchStreamptr: number, path: stringvoid切换直播地址
stopStreamptr: numbervoid关闭直播流
setStreamProgressptr: number, time: numbervoid设置直播进度条
isMuteptr: number, type: LwlPlayerType, mute: booleanvoid开/关声音
setRateptr: number, type: LwlPlayerType, rate: LwlVideoRatevoid视频倍速
playptr: number, path: string, seek: numbervoid开始播放(http&本地文件)
setProgressptr: number, time: numbervoid设置进度条(http&本地文件)
seekptr: number, path: string, start: number, end: numbervoid快进
stopptr: numbervoid停止播放
destroyptr: number, type: LwlPlayerTypevoid销毁播放器

场景示例

下面提供两个场景示例以供参考。

  • 场景一:单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

https://gitee.com/monad_c/lyzone-media.git

https://gitcode.com/Lyzone1024/lwlplayer.git

基于 Apache License 2.0 协议发行