使用AVPlayer实现视频播放器

使用AVPlayer实现视频播放器

HarmonyOS
2024-05-28 20:10:37
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
放大电路

本demo使用ArkTS语言实现一个视频播放器,可以实现本地视频的播放,并可以进行进度条和音量的设置

核心组件

  •  AVPlayer播放管理类:视频播放组件。
  • XComponent:可用于EGL/OpenGLES和媒体数据写入,并显示在XComponent组件。

核心代码

获取视频

export class HomeVideoListModel { 
  private videoLocalList: Array<VideoItem> = []; 
 
  // 获取本地视频 
  async getLocalVideo() { 
    this.videoLocalList = []; 
    await this.assemblingVideoBean(); 
    GlobalContext.getContext().setObject('videoLocalList', this.videoLocalList); 
    return this.videoLocalList; 
  } 
 
  // 组装本地视频对象 
  async assemblingVideoBean() { 
    VIDEO_DATA.forEach(async (item: VideoItem) => { 
      let videoBean = await getContext().resourceManager.getRawFd(item.iSrc); 
      let uri = videoBean; 
      this.videoLocalList.push(new VideoItem(item.name, uri, '')); 
    }); 
  } 
}

视频播放

XComponent组件初始化

创建AVPlayer实例对象并状态机绑定


Prepare状态

async firstPlay(index: number, url: resourceManager.RawFileDescriptor, iUrl: string, surfaceId: string) { 
    this.index = index; 
    this.url = url; 
    this.iUrl = iUrl; 
    this.surfaceId = surfaceId; 
    if (this.avPlayer === null) { 
        await this.createAVPlayer(); 
    } 
    if (this.avPlayer !== null) { 
        if (this.iUrl) { 
            this.avPlayer.url = this.iUrl; 
        } else { 
            this.avPlayer.fdSrc = this.url; 
        } 
    } 
}

绑定见bindState中AvplayerStatus.PREPARED

暂停

switchPlayOrPause() { 
    if (this.avPlayer === null) { 
        return; 
    } 
    if (this.status === CommonConstants.STATUS_START) { 
        this.avPlayer.pause(); 
    } else { 
        this.avPlayer.play(); 
    } 
}

绑定见bindState中AvplayerStatus.PLAYING和AvplayerStatus.PAUSED

播放速度、播放音量和播放位置

// 设置当前播放位置 
setSeekTime(value: number, mode: SliderChangeMode) { 
    if (mode === Number(SliderMode.MOVING)) { 
      this.progressThis.progressVal = value; 
      this.progressThis.currentTime = DateFormatUtil.secondToTime(Math.floor(value * this.duration / 
        CommonConstants.ONE_HUNDRED / CommonConstants.A_THOUSAND)); 
    } 
    if (mode === Number(SliderMode.END) || mode === Number(SliderMode.CLICK)) { 
      this.seekTime = value * this.duration / CommonConstants.ONE_HUNDRED; 
      if (this.avPlayer !== null) { 
        this.avPlayer.seek(this.seekTime, media.SeekMode.SEEK_PREV_SYNC); 
      } 
    } 
} 
 
// 音量设置 
onVolumeActionUpdate(event?: GestureEvent) { 
	if (!event) { 
	return; 
	} 
	if (this.avPlayer === null) { 
	return; 
	} 
	if (CommonConstants.OPERATE_STATE.indexOf(this.avPlayer.state) === -1) { 
	return; 
	} 
	if (this.playerThis.brightShow === false) { 
	this.playerThis.volumeShow = true; 
	    let screenWidth = GlobalContext.getContext().getObject('screenWidth') as number; 
	let changeVolume = (event.offsetX - this.positionX) / screenWidth; 
	    let volume: number = this.playerThis.volume; 
	let currentVolume = volume + changeVolume; 
	    let volumeMinFlag = currentVolume <= PlayConstants.MIN_VALUE; 
	let volumeMaxFlag = currentVolume > PlayConstants.MAX_VALUE; 
	    this.playerThis.volume = volumeMinFlag ? PlayConstants.MIN_VALUE : 
	(volumeMaxFlag ? PlayConstants.MAX_VALUE : currentVolume); 
	    this.avPlayer.setVolume(this.playerThis.volume); 
	this.positionX = event.offsetX; 
	} 
} 
 
// 设置播放速度 
setSpeed(playSpeed: number) { 
    if (this.avPlayer === null) { 
        return; 
    } 
    if (CommonConstants.OPERATE_STATE.indexOf(this.avPlayer.state) === -1) { 
        return; 
    } 
    this.playSpeed = playSpeed; 
    this.avPlayer.setSpeed(this.playSpeed); 
}
分享
微博
QQ
微信
回复
2024-05-29 20:35:07
相关问题
系统播放器AVplayer相关规格问题
336浏览 • 1回复 待解决
AVPlayer实现视频播放
699浏览 • 1回复 待解决
avplayer播放视频demo
1201浏览 • 1回复 待解决
HarmonyOS AvPlayer视频播放速度问题
279浏览 • 1回复 待解决
怎样实现一个自定义播放器
204浏览 • 1回复 待解决
java播放器怎么用解码?
4657浏览 • 1回复 待解决
播放器】硬解码支持的Demo
1696浏览 • 1回复 待解决
AVPlayer实现音频播放(c++侧)
1053浏览 • 1回复 待解决
如何适配网页内播放器全屏
527浏览 • 1回复 待解决
请提供HarmonyOS音频播放器实例代码
209浏览 • 0回复 待解决
AudioRenderer和播放器是什么关系?
3654浏览 • 1回复 待解决
HarmonyOS AVPlayer 播放问题
407浏览 • 1回复 待解决