#鸿蒙通关秘籍#如何为鸿蒙视频播放器添加暂停和播放功能?

HarmonyOS
2024-12-02 14:22:35
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
COO晨光熹

为了添加暂停和播放功能,可以在视频播放器类中定义 pause()play() 方法。暂停的方法通过调用 AVPlayerpause() 实现暂停,同时更新播放状态,而播放的方法通过调用 AVPlayerplay() 实现播放,同样需要更新播放状态。所有的状态更新应该调用 updateState() 方法以通知页面更新。代码示例如下:

import media from '@ohos.multimedia.media'

export class VideoAVPlayerClass {
  static player: media.AVPlayer | null = null
  static duration: number = 0
  static time: number = 0
  static isPlay: boolean = false
  static playList: videoItemType[] = []
  static playIndex: number = 0
  static surfaceId: string = ''

  static pause() {
    VideoAVPlayerClass.avPlayer.pause()
    VideoAVPlayerClass.isPlay = false
    VideoAVPlayerClass.updateState()
  }

  static async play() {
    VideoAVPlayerClass.avPlayer.play()
    VideoAVPlayerClass.isPlay = true
    VideoAVPlayerClass.updateState()
  }

  static async updateState() {
    const data = {
      playState: JSON.stringify({
        duration: VideoAVPlayerClass.duration,
        time: VideoAVPlayerClass.time,
        isPlay: VideoAVPlayerClass.isPlay,
        playIndex: VideoAVPlayerClass.playIndex,
        playList: VideoAVPlayerClass.playList,
      })
    }
    emitter.emit({
      eventId: EmitEventType.UPDATE_STATE
    }, {
      data
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
分享
微博
QQ
微信
回复
2024-12-02 15:54:48


相关问题
HarmonyOS 播放器后台暂停音频播放
393浏览 • 1回复 待解决
HarmonyOS 视频播放器问题
683浏览 • 1回复 待解决
HarmonyOS 播放器功能拓展
402浏览 • 1回复 待解决
使用AVPlayer实现视频播放器
1973浏览 • 1回复 待解决
HarmonyOS 视频播放器如何旋转屏幕
658浏览 • 1回复 待解决