HarmonyOS 播控中心的时间比AVPlayer获取到的时间要少1s

HarmonyOS
2025-01-10 08:56:43
585浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以参考一下,这里视频界面时长的数据来源和播控中心时长数据来源都是用的avPlayer.duration。

case 'prepared': // prepare调用成功后上报该状态机
console.info('AVPlayerDemo AVPlayer state prepared called.');
this.flag = true;
this.durationTime = Math.floor(avPlayer.duration / 1000);
this.durationStringTime = this.secondToTime(this.durationTime);
// avPlayer.loop = true;
avPlayer.setSpeed(media.PlaybackSpeed.SPEED_FORWARD_1_00_X)
avPlayer.seek(1, media.SeekMode.SEEK_PREV_SYNC)
await this.startAVSession();
break;

async startAVSession() {
  if (!this.currentAVSession) {
    console.error('AVPlayerDemo currentAVSession is undefined.')
    return;
  }
  let metadata: avSession.AVMetadata = this.generateAVMetadata();
  await this.currentAVSession.setAVMetadata(metadata).then(() => {
    console.info(`AVPlayerDemo SetAVMetadata successfully`);
  }).catch((err: BusinessError) => {
    console.error(`AVPlayerDemo SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`);
  });
  this.playbackState.state = avSession.PlaybackState.PLAYBACK_STATE_PREPARE;
  this.playbackState.duration = this.avPlayer?.duration;
  this.currentAVSession.setAVPlaybackState(this.playbackState);
  // 通过按钮申请长时任务
  this.startContinuousTask();
  this.currentAVSession.getController().then((avcontroller: avSession.AVSessionController) => {
    this.avsessionController = avcontroller;
    console.info(`AVPlayerDemo GetController : SUCCESS : sessionid : ${avcontroller.sessionId}`);
  }).catch((err: BusinessError) => {
    console.error(`AVPlayerDemo GetController BusinessError: code: ${err.code}, message: ${err.message}`);
  });
  await this.currentAVSession.activate().then(() => {
    console.info(`AVPlayerDemo Activate : SUCCESS `);
  }).catch((err: BusinessError) => {
    console.error(`AVPlayerDemo Activate BusinessError: code: ${err.code}, message: ${err.message}`);
  });
}

private generateAVMetadata() {
  let previousIndex = this.addStepIndex(-1);
  let nextIndex = this.addStepIndex(1);
  let metadata: avSession.AVMetadata = {
    assetId: `${this.sourceFiles[this.currentIndex].fd}`, // 必须,媒体ID。歌曲的唯一标识,由应用自定义。
    title: `${this.sourceFiles[this.currentIndex].fd}`, // 标题
    artist: `艺术家${this.currentIndex}`, // 艺术家
    author: `专辑作者${this.currentIndex}`, // 专辑作者
    album: `专辑名称${this.currentIndex}`, // 专辑名称
    writer: `词作者${this.currentIndex}`, // 词作者
    composer: `作曲者${this.currentIndex}`, // 作曲者
    duration: this.avPlayer?.duration, // 媒体时长,单位毫秒(ms)
    mediaImage: this.curPixelMap, // 图片的像素数据或者图片路径地址(本地路径或网络路径)
    publishDate: new Date(), // 发行日期
    subtitle: `子标题${this.currentIndex}`, // 子标题
    description: `媒体描述${this.currentIndex}`, // 媒体描述
    previousAssetId: `${this.sourceFiles[previousIndex].fd}`, // 上一首媒体ID
    nextAssetId: `${this.sourceFiles[nextIndex].fd}`, // 下一首媒体ID
  };
  return metadata;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
分享
微博
QQ
微信
回复
2025-01-10 10:40:47


相关问题
HarmonyOS date.getMonth() 预期值1
804浏览 • 1回复 待解决
HarmonyOS 如何获取当前时间时间
1069浏览 • 1回复 待解决
如何获取当前系统时间时间
2018浏览 • 1回复 待解决
ArkTS时间获取如何实现
5739浏览 • 1回复 已解决
HarmonyOS 如何获取文件创建时间
570浏览 • 1回复 待解决
HarmonyOS 怎么获取APP构建时间
763浏览 • 1回复 待解决
HarmonyOS 获取时间
1104浏览 • 1回复 待解决
HarmonyOS 获取当前时间
1191浏览 • 1回复 待解决