快速进阶
更新时间:2025-04-01
播放控制接口
// 开始播放或者继续播放均使用start接口。
this.$refs.bdplayerContainer.start();
// 暂停
this.$refs.bdplayerContainer.pause();
// seek到某个时间点,跳转到当前音视频播放的时间,单位秒,必须大于等于 0
this.$refs.bdplayerContainer.seekTo({
seconds: time
});
// 释放后,重新播放需创建新的player。
this.$refs.bdplayerContainer.stop();
获取当前播放状态
this.$refs.bdplayerContainer.isPlaying(null,(res)=>{
const { isPlaying } = res;
const title = isPlaying ? "正在播放中" : "已暂停";
});
获取音视频时长
this.$refs.bdplayerContainer.getDuration(null, ret => {
// 单位:s
console.log(ret.duration)
});
获取当前播放时间
this.$refs.bdplayerContainer.getCurrentPosition(null,ret=>{
// 单位:ms
console.log(ret.currentPosition)
})
设置/获取音量
// volume:number类型,音量大小,取值范围 0 ~ 1
this.$refs.bdplayerContainer.setVolume({
volume: 0.5
});
this.$refs.bdplayerContainer.getVolume(null, ret => {
const {volume} = ret
});
设置/获取倍速
// speed: number类型,取值范围 0.5 ~ 2.0,默认值 1.0
this.$refs.bdplayerContainer.setSpeed({
speed:1.5
});
this.$refs.bdplayerContainer.getSpeed(null, ret => {
const {speed} = ret
});
切换屏幕方向到横/竖屏
isFull ? this.$refs.bdplayerContainer.changeToLandscape() : this.$refs.bdplayerContainer.changeToPortrait();
截图
保存播放器播放视频当前画面截图到相册。要开启相册权限。
this.$refs.bdplayerContainer.snapshot(null, result => {
if (result.success) {
uni.showToast({ title: '播放器截屏成功' });
} else {
uni.showToast({ title: '失败: ' + result.error, icon: 'none' });
}
});
设置画面填充模式
this.$refs.bdplayerContainer.setScalingMode({scalingMode:1});
scalingMode
- 类型:number类型
- 描述:(可选项)拉伸模式, 取值范围1-填充, 2-裁剪, 3-铺满。默认为1-填充。
切换清晰度
this.$refs.bdplayerContainer.changeLevel({
level:index
},(res)=> {
const { errMsg } = res;
if(!errMsg){
this.isChangeLevel = true;
uni.showToast({
title: "正在切换清晰度...",
icon: "none"
})
}
});
level
- 类型:整型数字
- 描述:(可选项)指定码率,取值范围 1, 2, 3。level值为:onLevelUpdate事件返回清晰度下标
播放私有加密视频
this.$refs.bdplayerContainer.setUp({
file: this.defaultConfig.file,
token:this.defaultConfig.token,
}, (ret) => {
this.text = JSON.stringify(ret);
if (ret.errMsg != null) {
uni.showToast({
title: ret.errMsg,
icon: "none"
})
}
});
token
token需要您的服务器与百度智能云服务器合作来生成,您的App从您的服务器拿到token后,设置给播放器即可。
播放自定义事件监听
播放状态变化事件监听
onPlayStatus
playerState
- 类型:字符串
- 描述:「prepared-已准备完成, pause-暂停, stop-停止,playing-播放」
seek
类型:字符串 描述:「complete-seek完成」
onPlayStatus(e) {
const {
skin,
bdPlayer
} = this;
const state = e.detail.playerState;
const preparedToPlay = e.detail.preparedToPlay;
const seekstate = e.detail.seek;
if (state != null) {
this.skin.changePlayStatus(state === 'playing');
if(state == "prepared"){
uni.showToast({
title: "视频已准备就绪",
icon: "none"
})
}else if(state == "complete"){
console.log("播放完成");
uni.showToast({
title: "播放完成",
icon: "none"
})
}else if(state == "pause"){
uni.showToast({
title: "视频已暂停",
icon: "none"
})
}else if(state == "playing"){
uni.showToast({
title: "视频已播放",
icon: "none"
})
}
} else if (preparedToPlay != null) {
this.updateDuration();
}else if(seekstate != null){
if(seekstate == "complete"){
uni.showToast({
title: "seek完成",
icon: "none"
})
}
}
}
视频分辨率改变
onVideoSizeChanged 视频分辨率改变通知
width
- 类型:number
- 描述:返回当前视频宽度
height
- 类型:number
-
描述:返回当前视频高度
onVideoSizeChanged(e){ const {width,height} = e.detail; if(this.isChangeLevel){ uni.showToast({ title: "清晰度切换成功", icon: "none" }) } console.log(`width is ${width},height is ${height}`); }
当前播放视频所支持码率
onLevelUpdate
resolution
- 类型:array
-
描述:返回当前播放视频所支持码率列表。参数["360p","720p"]
onLevelUpdate(e){ const {resolution} = e.detail; this.$refs.skin.updateLevels(resolution); console.log(`resolution is ${resolution}`,); }
错误事件
onPlayError
error
- 类型:string
-
描述:错误信息
onPlayError(e){ const {error} = e.detail; }