AVPlayer组件莫名其妙无法播放
已解决
为什么说莫名其妙?他怪就怪在明明之前能跑通一点问题没有,我把项目搁置了几天再部署,他就异常了
我保证我一点代码都没动,用Git回溯仓库里的可运行版本也无济于事,而且已经是第二次出现这种情况了
具体表现是播放Resfile内的WAV文件,在prepare阶段报错并无法播放:
E #918 SignError: failed to prepare, avplayer enter error status, please check error callback messages!
E #834 OnErrorCb:errorCode 5400106, errorMsg Unsupported Format: CONTAINER_ERR-null-unsupport interface, unsupport container format type
W [source_map.cpp145]the stack without line info在此报错前还会出现两段警告:
W #72 Getxattr value failed, errno is No data available
W #62 === fd is cloud扔给AI分析它往往都会认为这两段警告是关键,但我实在是找不到是哪的问题
我的AVPlayer组件核心代码如下:
export class audioPlayer {
public avPlayer?: media.AVPlayer
private historyFilePath: string = ''//用于校验播放的文件是否与上次相同
private playerState: media.AVPlayerState = 'idle'; // 当前播放器状态
private waitPlay: boolean = false; // 是否等待播放(用于状态未准备好时的延迟播放)
private playerStatesEnum: string[] = ['prepared', 'playing', 'paused', 'completed']
static audioPlayerInstance: audioPlayer | undefined; // 单例实例
private SBReporter = new SBReporter(0x0001, 'audioPlayer')
//加载歌曲资源文件描述符
async loadAudioAssent(path: string) {
try {
let audioFDPath: string = `fd://${(await fileIo.open(path)).fd || ''}`
this.SBReporter.value('加载歌曲资源文件描述符-audioFDPath', audioFDPath)
if (!this.avPlayer) {
return
}
this.avPlayer.url = audioFDPath
this.SBReporter.info('加载歌曲资源文件描述符-audioFDPath', 'URL设置成功:')
} catch (error) {
this.SBReporter.error('加载歌曲资源文件描述符-audioFDPath', error)
}
}
//此处省略播放器初始化和回调设置
//准备播放器(主要报错位置)
public async prepare() {
if (!this.avPlayer) {
this.SBReporter.warn('AVPlayer-校验', 'AVPlayer不存在')
return
}
await this.avPlayer.prepare().catch((prepareErr: BusinessError) => {
this.SBReporter.error('prepare-校验', prepareErr)
}).then(() => {
// 存储总时长(格式化后的字符串)
AppStorage.setOrCreate('totalTime', MediaTools.msToCountdownTime(this.avPlayer?.duration!));
// 存储总时长(毫秒)
AppStorage.setOrCreate('totalMsTime', this.avPlayer?.duration!);
// 存储进度条最大值
AppStorage.setOrCreate('progressMax', this.avPlayer?.duration!);
})
}
//播放
public async play() {
if (!this.avPlayer) {
this.SBReporter.warn('AVPlayer-校验', 'AVPlayer不存在')
return;
}
// 检查当前状态是否允许播放
if (!this.playerStatesEnum.includes(this.playerState)) {
this.waitPlay = true; // 标记等待播放,状态准备好后自动播放
this.SBReporter.warn('play', 'AVPlayer不处于可播放状态')
return;
}
try {
await this.avPlayer.play();
} catch (error) {
this.SBReporter.error('play', error)
}
this.waitPlay = false
this.updateIsPlay(true)
AppStorage.setOrCreate<boolean>('isPlay', true) // 更新全局播放状态
}
//此处省略其他无关紧要的业务逻辑
//智能播放,可自动处理不同状态下的播放逻辑
//调用AVPlayer时会直接调用它
public async smartPlay(path: string) {
if (!this.avPlayer) {
this.SBReporter.warn('AVPlayer-校验', 'AVPlayer不存在')
return;
}
if (this.historyFilePath == path) {
this.SBReporter.info('智能播放', `播放文件不变,不切换文件 当前状态:${this.playerState}`)
//播放文件不变,不再切换文件,直接调整进度
switch (this.playerState) {
case 'playing':
this.avPlayer.seek(0)
break;
case 'paused':
case 'completed':
case 'prepared':
this.play()
break;
}
} else {
this.SBReporter.info('智能播放', `播放文件改变,切换文件后播放 当前状态:${this.playerState}`)
this.historyFilePath = path;
//播放文件改变,切换文件
switch (this.playerState) {
case 'idle':
await this.loadAudioAssent(path)
this.waitPlay = true
break;
case 'initialized':
this.waitPlay = true
break;
case 'prepared':
this.play()
break;
case 'playing':
case 'paused':
case 'completed':
await this.stop()
await this.loadAudioAssent(path)
this.waitPlay = true
break;
case 'stopped':
await this.loadAudioAssent(path)
this.waitPlay = true
break;
case 'error':
try {
await this.avPlayer.reset();
this.SBReporter.info('智能播放','重置AVPlayer成功')
} catch (error) {
this.SBReporter.error('智能播放',error)
}
await this.loadAudioAssent(path);
this.waitPlay = true;
break;
}
}
//此处省略各种回调逻辑
private updateIsPlay(isPlay: boolean) {
AppStorage.setOrCreate<boolean>('isPlay', isPlay); // 更新全局播放状态
this.SBReporter.info('更新全局播放状态', `${isPlay}`)
}
}
}调用端只有很简单的几行:
aboutToAppear(): void {
try {
application.createModuleContext(this.getUIContext().getHostContext(), 'default')
.then((targetContext: Context) => {
this.targetDirSandboxPath = targetContext.resourceDir + '/' + this.relativePath
this.classStrings = fs.listFileSync(this.targetDirSandboxPath, { recursion: this.scanChildren })
})
//初始化AVPlayer
audioPlayer.getInstance().initAVPlayer()
} catch (error) {
this.SBReporter.error('生成音效上下文路径', error)
}
}
//build
//通过onclick触发并获取路径,路径前缀均为/data/storage/el1/bundle/default/resources/resfile/
if (pathIsDirectory) {
this.navStack?.pushPath({
name: 'ContentPage', param:pushParamData})
} else {
this.SBReporter.value('文件列表', '这不是目录,尝试打开')
audioPlayer.getInstance().smartPlay(`${this.targetDirSandboxPath}/${item}`)
}
我测试过两台设备,一个系统是7.0.0.23 Developer Beta,一个是6.1.0.117,均复现问题,我实在是没辙了找不到一点解决方向,遂来向广大社区大佬们求助,能帮上忙的话本人感激不尽,如有需要我可以把gitee仓库公开以供查看,新人代码水平不精求轻喷
精简后的日志原文在附件中
3
5
浏览219 发布于2026-07-09 09:18河北
全部评论
最多点赞
最新发布
最早发布
写回答
所见即所得编辑器, editor1
工具栏
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
了解社区公约,与您携手共创和谐专业的开发者社区。












































苏公安网备 32011402010933号