登录

去注册 忘记密码?

登录

注册

去登录

  • 扫码关注公众号
  • 发送“我爱安卓
  • 即可获取验证码

注册

解锁回答区域

  • 扫码关注公众号
  • 发送“我爱安卓

若你登陆,将永久解锁;
若未登录,仅本机解锁。

解锁回答区域

获取注册验证码

  • 扫码关注公众号
  • 发送“我爱安卓
  • 即可获取验证码

HmVideoPlayer:基于HarmonyOSNext SDK 一款视频播放器

xufei5789651   2026-05-15 21:34   收藏

HmVideoPlayer

基于HarmonyOSNext SDK 一款视频播放器

特性

  • 视频编解码:支持H.264、H.265
  • 流媒体:支持HTTP/HTTPS、HLS协议
  • 支持HDR Vivid、Audio Vivid 播放
  • 支持倍速播放

依赖方式

ohpm install @glodentime/hmvideoplayer

使用示例

  • 基础用法

XComponent({
  id: 'player',
  type: XComponentType.SURFACE,
  libraryname: 'hmvideoplayer',
  controller: this.xComponentController
})
  .onLoad(() => {
    this.xComponentController.setXComponentSurfaceRect({ surfaceWidth: WIDTH_PX, surfaceHeight: HEIGHT_PX });
    HmVideoPlayer.initWithURL(this.URL);
  })
  .width('100%')
  .height('100%')


onPageShow():void {
  this.setVideoPlayerCallback();
}

setVideoPlayerCallback() {
    // 注册播放状态变更监听
    HmVideoPlayer.onStateChange((state: number) => {
      switch (state) {
        case StateCode.PREPARED:
          this.isPlay = false;
          this.setVideoDuration();
          HmVideoPlayer.play();
          break;
        case StateCode.PLAYING:
          this.isPlay = true;
          this.setUpdateTimeCallback();
          break;
        case StateCode.PAUSED:
          this.isPlay = false;
          break;
        case StateCode.STOPPED:
          this.isPlay = false;
          break;
        case StateCode.COMPLETED:
          this.isPlay = false;
          break;
        case StateCode.RELEASE:
          this.isPlay = false;
          break;
      }
    });

    // 注册音频焦点变化监听
    HmVideoPlayer.onAudioInterrupt((forceType: number, hint: number) => {
      if (forceType === audio.InterruptForceType.INTERRUPT_FORCE) {
        switch (hint) {
          case audio.InterruptHint.INTERRUPT_HINT_PAUSE:
            HmVideoPlayer.pause();
            break;
          case audio.InterruptHint.INTERRUPT_HINT_STOP:
            HmVideoPlayer.pause();
            break;
        }
      } else if (forceType === audio.InterruptForceType.INTERRUPT_SHARE) {
        switch (hint) {
          case audio.InterruptHint.INTERRUPT_HINT_RESUME:
            HmVideoPlayer.resume();
            break;
        }
      }
    });

    // 注册音频错误监听
    HmVideoPlayer.onAudioError((errorAudioCode: number) => {
      console.log(TAG,`errorAudioCode:${errorAudioCode}`);
    });
    
    // 注册输出设备变化监听
    HmVideoPlayer.onOutputDeviceChange((deviceChange: number) => {
      if (deviceChange === audio.AudioStreamDeviceChangeReason.REASON_OLD_DEVICE_UNAVAILABLE) {
        HmVideoPlayer.pause();
      } else if (deviceChange === audio.AudioStreamDeviceChangeReason.REASON_NEW_DEVICE_AVAILABLE) {
        HmVideoPlayer.resume();
      }
    })
    
    // 注册音视频编解码运行错误监听
    HmVideoPlayer.onCodecError((errorAvcodecCode: number) => {
      console.log(TAG,`errorAvcodecCode:${errorAvcodecCode}`);
    });

    // 注册码流信息变化,如:声道变化等
    HmVideoPlayer.onCodecFormatChange((videoWidth: number, videoHeight: number, audioSampleFormat: number,
      audioChannelCount: number, audioSampleRate: number, videoFrameRate: number) => {
      console.log(TAG,`videoWidth:${videoWidth},videoHeight:${videoHeight},audioSampleFormat:${audioSampleFormat},
      videoFrameRate:${videoFrameRate},audioChannelCount:${audioChannelCount},audioSampleRate:${audioSampleRate}`);
    });
}

// 获取视频时长
private setVideoDuration()
{
  this.duration = Math.floor(HmVideoPlayer.getDuration() / SECOND_TO_MS);
}

// 监听时间戳变化
private setUpdateTimeCallback()
{
  HmVideoPlayer.onTimeUpdate((timestamp: number) => {
    this.currentTimestamp = Math.floor(timestamp / SECOND_TO_MS);
  });
}

onPageHide():void {
  HmVideoPlayer.stop();
  HmVideoPlayer.release();
}

接口能力

HmVideoPlayer 方法

接口参数返回值说明
initWithURLurl: stringvoid加载网络资源
initWithLocalinputFileFd: number,inputFileOffset: number,inputFileSize: numbervoid加载本地视频
playvoidvoid开始播放
pausevoidvoid暂停播放
resumevoidvoid恢复播放
stopvoidvoid停止播放
releasevoidvoid释放播放器
getDurationvoidnumber获取媒体资源的总时长
seekposition: bigintvoid跳转至指定进度
onTimeUpdatecallback: (timestamp: number) => voidvoid获取当前播放进度时间戳
onStateChangecallback: (state: number) => voidvoid注册播放状态变更监听
onAudioInterruptcallback: (forceType: number, hint: number) => voidvoid注册音频焦点变化监听
onOutputDeviceChangecallback: (deviceChange: number) => voidvoid注册输出设备变化监听
onAudioErrorcallback: (errorAudioCode: number) => voidvoid注册音频错误监听
onCodecFormatChangecallback: (videoWidth: number, videoHeight: number, audioSampleFormat: number,audioChannelCount: number, audioSampleRate: number, videoFrameRate: number) => voidvoid注册码流信息变化,如:声道变化等
onCodecErrorcallback: (errorAvcodecCode: number) => voidvoid注册音视频编解码运行错误监听
ratePlayspeed: numbervoid倍速播放

开源协议

本项目基于 Apache License 2.0 ,在拷贝和借鉴代码时,请大家务必注明出处。

项目地址:https://github.com/xufei5789651/HmVideoPlayer