HarmonyOS 使用XComponent和AvPlayer播放视频出现有声音无画面问题
参考代码:
Row() {
XComponent({ id: 'xcomponent', type: XComponentType.SURFACE, controller: this.xcomponentController })
.onLoad(() => {
this.surfaceID = this.xcomponentController.getXComponentSurfaceId()
Logger.info(TAG, "surfaceId=" + this.surfaceID);
this.xComponentContext = this.xcomponentController.getXComponentContext() as Record<string, () => void>
let surfaceRect: SurfaceRect = { offsetX: 0, offsetY: 0, surfaceWidth: 500, surfaceHeight: 500 }
this.xcomponentController.setXComponentSurfaceRect(surfaceRect)
this.rect = this.xcomponentController.getXComponentSurfaceRect()
}).width('100%')
.height(260)
}
.width("95%").backgroundColor("#12638bc1").border({ width: 1, color: Color.Pink })
.justifyContent(FlexAlign.Center)
@Component
export struct VideoControls {
private context = getContext(this) as common.UIAbilityContext
@Consume('previewObject') item: fileItem
@Link avPlayer: media.AVPlayer | undefined;
@Consume surfaceID: string;
// 视频播放倍速
@Consume currentProgressRate: number
// SR开关
@Consume srSwitch: boolean
// 是否横屏播放
@StorageLink(AppConfig.isLandscape) isLandscape: boolean = AppStorage.get(AppConfig.isLandscape) || false
@State count: number = 0;
@State durationTime: number = 0;
build() {
Flex({
justifyContent: FlexAlign.SpaceBetween,
alignItems: ItemAlign.Center,
}) {
// 播放按钮
Image(this.item.play ? $r('app.media.icon_pause_white') : $r('app.media.icon_play'))
.width(px2vp(54))
.height(px2vp(54))
.objectFit(ImageFit.Contain)
.onClick(() => {
this.iconOnclick();
})
// 进度条
Text(this.item.currentStringTime)
.fontSize(px2fp(40))
.fontColor('#BFBFBF')
Slider({
value: this.count,
min: 0,
max: this.durationTime,
step: 1,
style: SliderStyle.OutSet
})
.blockColor('#ffffff')
.width('46.7%')
.trackColor(Color.Gray)
.selectedColor('#ffffff')
.showSteps(true)
.showTips(true)
.trackThickness(4)
.onChange((value: number) => {
this.sliderOnchange(value);
})
Text(this.durationTime.toString())
.fontSize(px2fp(40))
.fontColor('#BFBFBF')
if (!this.isLandscape) {
Image($r('app.media.icon_fullscreen'))
.width(px2vp(100))
.height(px2vp(100))
.objectFit(ImageFit.Contain)
.onClick(() => {
this.isLandscape = true
})
}
}
.height(px2vp(107.5))
.padding({
left: px2vp(13.4),
right: px2vp(13.4),
})
}
/**
* 初始化 AvPlayer
*/
private initAvPlayer(surfaceId: string) {
Logger.info(TAG, `x compoent surface id=${this.surfaceID}`)
media.createAVPlayer().then((player) => {
this.avPlayer = player;
this.setAVPlayerCallback(this.avPlayer)
const origin_path: Resource = this.item.path as Resource;
let video_path: string = ""
if (origin_path.params != undefined) {
video_path = origin_path.params[0] as string;
}
Logger.info(TAG, `video_path: ${video_path}`)
this.context.resourceManager.getRawFd(video_path)
.then((file) => {
let avFileDescriptor: media.AVFileDescriptor =
{ fd: file.fd, offset: file.offset, length: file.length };
if (this.avPlayer != undefined) {
this.avPlayer.fdSrc = avFileDescriptor;
this.avPlayer.surfaceId = surfaceId;
}
})
});
}
private setAVPlayerCallback(avPlayer: media.AVPlayer) {
avPlayer.on('stateChange', async (state: string, reason: media.StateChangeReason) => {
switch (state) {
case 'initialized': // avplayer 设置播放源后触发该状态上报
avPlayer.prepare();
break;
case 'prepared': // prepare调用成功后上报该状态机
Logger.info(TAG, 'AVPlayer state prepared called.');
this.count = 0;
this.durationTime = avPlayer.duration;
avPlayer.play(); // 调用播放接口开始播放
break;
case "playing":
this.item.play = true;
this.count += 1 ;
break;
case 'completed':
this.item.play = false;
break;
case 'paused':
this.item.play = false;
break;
}
})
}
iconOnclick() {
if (this.avPlayer == undefined) {
this.initAvPlayer(this.surfaceID)
} else {
if (this.item.play === true) {
this.item.controller?.pause()
this.avPlayer?.pause();
this.item.play = false;
return;
} else {
this.item.controller?.start();
this.avPlayer?.play((err) => {
if (err == null) {
Logger.info(TAG, ' play success');
} else {
console.error(TAG, `video pause error:${err.message}`)
}
})
this.item.play = true;
}
}
}
sliderOnchange(value: number) {
this.item.currentTime = Number.parseInt(value.toString());
if (this.avPlayer != undefined) {
this.avPlayer.seek(value, media.SeekMode.SEEK_NEXT_SYNC);
}
this.item.controller?.setCurrentTime(Number.parseInt(value.toString()), SeekMode.Accurate);
}
}
HarmonyOS
赞
收藏 0
回答 1
待解决
相关问题
HarmonyOS XComponent和AVplayer实现视频播放,视频被拉伸
46浏览 • 1回复 待解决
HarmonyOS 使用avPlayer和XComponent实现视频播放器的一些问题
40浏览 • 1回复 待解决
HarmonyOS AvPlayer视频播放速度问题
492浏览 • 1回复 待解决
HarmonyOS XComponent播放视频问题
44浏览 • 1回复 待解决
ubuntu19有声音输出,但是没有声音输入
1846浏览 • 1回复 待解决
使用AVPlayer实现视频播放器
1331浏览 • 1回复 待解决
HarmonyOS 在web组件中播放音频没有声音,手指触摸一下h5页面才有声音
73浏览 • 1回复 待解决
AVPlayer实现视频播放
1071浏览 • 1回复 待解决
avplayer播放视频demo
1615浏览 • 1回复 待解决
HarmonyOS 视频播放AVPlayer解码异常
46浏览 • 1回复 待解决
HarmonyOS AVPlayer 播放问题
766浏览 • 1回复 待解决
HarmonyOS 关于AVPlayer设置播放本地视频失败的问题
30浏览 • 1回复 待解决
HarmonyOS 使用Avplayer播放音乐,同时再播放其他音效会导致正在播放的音乐声音变小然后恢复原来的声音大小
70浏览 • 1回复 待解决
HarmonyOS AVPlayer XComponent
375浏览 • 1回复 待解决
HarmonyOS 打开人脸活体检测页面有声音,如何控制声音开关?
385浏览 • 1回复 待解决
HarmonyOS AVPlayer播放视频,退出页面后台播放时,视频会停止
542浏览 • 1回复 待解决
ArkTS使用AVPLayer实现视频播放时,如何实现全屏播放功能
2491浏览 • 0回复 待解决
ijkplayer结合XComponent组件播放视频时根据视频尺寸和组件尺寸设置视频缩放模式
2028浏览 • 0回复 待解决
HarmonyOS 通过xcomponent + avplayer实现直播流的播放以及实现类似短视频app的上下翻页切换视频的效果的逻辑以及源码,xcomponent + avplayer实现的视频播放器能否做成组件并插入到信息流中
35浏览 • 1回复 待解决
HarmonyOS soundpool播放声音的问题
587浏览 • 1回复 待解决
js采集声音出现问题怎么处理?
3367浏览 • 1回复 待解决
动态缩放XComponent大小和视频播放时的性能对比
2057浏览 • 1回复 待解决
HarmonyOS 在使用AVplayer中如何自定义播放视频的大小?
347浏览 • 1回复 待解决
HarmonyOS 视频使用AVPlayer播放,如果在暂停状态更换surfaceId会变成黑屏
33浏览 • 1回复 待解决
HarmonyOS ohaudio native播放音频声音有问题
93浏览 • 1回复 待解决
surfaceId需要在avPlayer的initialized状态下赋值绑定,请参考: