使用AVPlayer实现视频播放器

使用AVPlayer实现视频播放器

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

本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实现视频播放
284浏览 • 1回复 待解决
avplayer播放视频demo
356浏览 • 1回复 待解决
AVPlayer实现音频播放(c++侧)
403浏览 • 1回复 待解决
播放器】硬解码支持的Demo
634浏览 • 1回复 待解决
java播放器怎么用解码?
3660浏览 • 1回复 待解决
如何适配网页内播放器全屏
271浏览 • 1回复 待解决
AudioRenderer和播放器是什么关系?
2638浏览 • 1回复 待解决
AVplayer开发音频播放功能
477浏览 • 1回复 待解决
怎么基于Java实现视频播放
1584浏览 • 1回复 待解决
如何实现RTSP视频播放
260浏览 • 1回复 待解决
播放器API-timeUpdate-时间单位不清楚
561浏览 • 1回复 待解决