|
|
@@ -7306,22 +7306,93 @@ export struct LocalMusic {
|
|
|
this.mDestroyPage = false;
|
|
|
this.animationState = AnimationStatus.Running
|
|
|
LogUtils.getInstance().LOGI("startPlayOrResumePlay start this.CONTROL_PlayStatus:" + this.CONTROL_PlayStatus)
|
|
|
+
|
|
|
+ // 检查并更新当前歌曲的元数据
|
|
|
+ if (this.currentSong && (!this.currentSong.sampleRate || !this.currentSong.mimeType)) {
|
|
|
+ // 异步检查元数据,不阻塞播放流程
|
|
|
+ this.checkAndUpdateMetadata();
|
|
|
+ }
|
|
|
+
|
|
|
if (this.CONTROL_PlayStatus == PlayStatus.INIT) {
|
|
|
this.stopProgressTask();
|
|
|
this.startProgressTask();
|
|
|
- this.play(this.videoUrl.toString());
|
|
|
-
|
|
|
-
|
|
|
+ // 确保 videoUrl 不为 undefined
|
|
|
+ if (this.videoUrl) {
|
|
|
+ this.play(this.videoUrl.toString());
|
|
|
+ } else {
|
|
|
+ LogUtils.getInstance().LOGI("无法播放:videoUrl 为空");
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
if (this.CONTROL_PlayStatus == PlayStatus.PAUSE) {
|
|
|
this.mIjkMediaPlayer.start();
|
|
|
this.setProgress()
|
|
|
this.CONTROL_PlayStatus = PlayStatus.PLAY
|
|
|
}
|
|
|
+
|
|
|
this.setIsPlaying(true)
|
|
|
this.updateSessionPlayState(true)
|
|
|
this.watchStatus();
|
|
|
}
|
|
|
+
|
|
|
+ // 检查并更新元数据的辅助方法
|
|
|
+ private checkAndUpdateMetadata() {
|
|
|
+ // 首先检查 currentSong 是否存在
|
|
|
+ if (!this.currentSong || StrUtil.isEmpty(this.currentSong.filePath)) {
|
|
|
+ console.error('无法更新元数据,当前歌曲或文件路径为空');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建本地变量存储当前歌曲信息
|
|
|
+ const song = this.currentSong;
|
|
|
+ const filePath = song.filePath;
|
|
|
+
|
|
|
+ // 使用QueryCallback类型的函数变量明确处理回调
|
|
|
+ let callback = (result: VideoItem[]) => {
|
|
|
+ // 检查当前歌曲是否仍然存在
|
|
|
+ if (!this.currentSong) {
|
|
|
+ console.error('回调期间当前歌曲已变为空');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result && result.length > 0) {
|
|
|
+ const dbItem = result[0];
|
|
|
+ if (dbItem.sampleRate || dbItem.mimeType) {
|
|
|
+ // 使用try-catch块确保即使出现异常也不会中断程序
|
|
|
+ try {
|
|
|
+ // 再次检查currentSong是否存在
|
|
|
+ if (this.currentSong) {
|
|
|
+ let currentSong = this.currentSong;
|
|
|
+ currentSong.sampleRate = dbItem.sampleRate;
|
|
|
+ currentSong.mimeType = dbItem.mimeType;
|
|
|
+ currentSong.duration = dbItem.duration;
|
|
|
+ currentSong.trackCount = dbItem.trackCount;
|
|
|
+ console.info(`从数据库加载歌曲元数据 - 采样率: ${dbItem.sampleRate}, MIME类型: ${dbItem.mimeType}`);
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ console.error(`更新元数据时出错: ${e}`);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 数据库中也没有,从文件提取
|
|
|
+ if (this.currentSong) {
|
|
|
+ this.refreshCurrentSongMetadata();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 数据库中没有这首歌,从文件提取
|
|
|
+ if (this.currentSong) {
|
|
|
+ this.refreshCurrentSongMetadata();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 使用try-catch块包裹数据库查询
|
|
|
+ try {
|
|
|
+ this.table.queryByFilePath(filePath, callback);
|
|
|
+ } catch (e) {
|
|
|
+ console.error(`查询数据库时出错: ${e}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private completionNum(num: number): string | number {
|
|
|
if (num < 10) {
|
|
|
@@ -7378,7 +7449,10 @@ export struct LocalMusic {
|
|
|
position = duration;
|
|
|
}
|
|
|
this.isCurrentTime = true;
|
|
|
- this.lyricController.updatePosition(position + this.timeOffset * 1000)
|
|
|
+ // 确保 lyricController 不为空
|
|
|
+ if (this.lyricController) {
|
|
|
+ this.lyricController.updatePosition(position + this.timeOffset * 1000);
|
|
|
+ }
|
|
|
this.currentTime = this.stringForTime(position);
|
|
|
this.isCurrentTime = false
|
|
|
|
|
|
@@ -7434,27 +7508,40 @@ export struct LocalMusic {
|
|
|
|
|
|
// 从文件重新提取音频元数据
|
|
|
private async refreshCurrentSongMetadata() {
|
|
|
+ // 先检查currentSong是否存在
|
|
|
if (!this.currentSong || StrUtil.isEmpty(this.currentSong.filePath)) {
|
|
|
console.error('无法刷新元数据,当前歌曲或文件路径为空');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // 创建本地变量存储必要信息
|
|
|
+ const song = this.currentSong;
|
|
|
+ const filePath = song.filePath;
|
|
|
+
|
|
|
try {
|
|
|
// 直接从音频文件重新加载完整的元数据
|
|
|
- console.info(`开始重新提取音频元数据: ${this.currentSong.filePath}`);
|
|
|
+ console.info(`开始重新提取音频元数据: ${filePath}`);
|
|
|
const refreshedItem = await Utility.uriGetMusicAssetsFromFile(
|
|
|
this.context,
|
|
|
- this.currentSong.filePath,
|
|
|
+ filePath,
|
|
|
CommonConstants.TYPE_LOCAL,
|
|
|
false
|
|
|
);
|
|
|
|
|
|
- // 只更新元数据相关字段,保留其他字段
|
|
|
- if (this.currentSong) {
|
|
|
- this.currentSong.duration = refreshedItem.duration;
|
|
|
- this.currentSong.mimeType = refreshedItem.mimeType;
|
|
|
- this.currentSong.sampleRate = refreshedItem.sampleRate;
|
|
|
- this.currentSong.trackCount = refreshedItem.trackCount;
|
|
|
+ // 异步操作结束后再次检查currentSong是否仍然存在
|
|
|
+ if (!this.currentSong) {
|
|
|
+ console.error('异步操作完成后,当前歌曲已变为空');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用try-catch块确保即使出现异常也不会中断程序
|
|
|
+ try {
|
|
|
+ // 再次检查currentSong是否存在并更新字段
|
|
|
+ const currentSong = this.currentSong; // 刷新引用
|
|
|
+ currentSong.duration = refreshedItem.duration;
|
|
|
+ currentSong.mimeType = refreshedItem.mimeType;
|
|
|
+ currentSong.sampleRate = refreshedItem.sampleRate;
|
|
|
+ currentSong.trackCount = refreshedItem.trackCount;
|
|
|
|
|
|
// 更新数据库中的记录以确保下次不需要重新提取
|
|
|
// 创建符合MediaMetadata接口的对象
|
|
|
@@ -7464,15 +7551,29 @@ export struct LocalMusic {
|
|
|
sampleRate: refreshedItem.sampleRate,
|
|
|
trackCount: refreshedItem.trackCount
|
|
|
};
|
|
|
- this.table.updateMediaMetadata(this.currentSong.filePath, metadataToUpdate, (success: boolean) => {
|
|
|
- if (success) {
|
|
|
- console.info('成功更新音频元数据到数据库');
|
|
|
- } else {
|
|
|
- console.error('更新音频元数据到数据库失败');
|
|
|
+
|
|
|
+ // 定义数据库更新回调
|
|
|
+ let updateCallback = (success: boolean) => {
|
|
|
+ try {
|
|
|
+ if (success) {
|
|
|
+ console.info('成功更新音频元数据到数据库');
|
|
|
+ } else {
|
|
|
+ console.error('更新音频元数据到数据库失败');
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ console.error(`数据库更新回调中出错: ${e}`);
|
|
|
}
|
|
|
- });
|
|
|
+ };
|
|
|
|
|
|
- console.info(`刷新后的采样率: ${this.currentSong.sampleRate}, MIME类型: ${this.currentSong.mimeType}`);
|
|
|
+ // 使用try-catch块包裹数据库更新操作
|
|
|
+ try {
|
|
|
+ this.table.updateMediaMetadata(filePath, metadataToUpdate, updateCallback);
|
|
|
+ console.info(`刷新后的采样率: ${currentSong.sampleRate}, MIME类型: ${currentSong.mimeType}`);
|
|
|
+ } catch (dbErr) {
|
|
|
+ console.error(`更新数据库时出错: ${dbErr}`);
|
|
|
+ }
|
|
|
+ } catch (updateErr) {
|
|
|
+ console.error(`更新元数据字段时出错: ${updateErr}`);
|
|
|
}
|
|
|
} catch (err) {
|
|
|
console.error(`刷新音频元数据失败: ${err}`);
|