#鸿蒙通关秘籍#如何在鸿蒙视频播放器中实现手动切换视频的功能?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
雨后彩虹GUI

为了在视频列表中手动切换视频,可以在播放器类中添加 singlePlay() 方法。在该方法中通过检查要切换的视频是否已经存在于播放列表中来决定添加还是直接跳转到该视频。以下代码展示了如何实现手动切换功能:

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 singlePlay(video?: videoItemType) {
    if (video) {
      let index = VideoAVPlayerClass.playList.findIndex((item: videoItemType) => item.id === video.id)
      if (index > -1) {
        VideoAVPlayerClass.playIndex = index
      } else {
        VideoAVPlayerClass.playList.push(video)
        VideoAVPlayerClass.playIndex = VideoAVPlayerClass.playList.length - 1
      }
    }
    VideoAVPlayerClass.changePlay()
  }

  static async changePlay() {
    await VideoAVPlayerClass.avPlayer.reset()
    VideoAVPlayerClass.time = 0
    VideoAVPlayerClass.duration = 0
    VideoAVPlayerClass.avPlayer.url = VideoAVPlayerClass.playList[VideoAVPlayerClass.playIndex].url
    VideoAVPlayerClass.updateState()
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
使用AVPlayer实现视频播放器
1155浏览 • 1回复 待解决
HarmonyOS 点播视频播放器选型咨询
178浏览 • 1回复 待解决