1

onPlaybackStatusUpdate 应在每 100 毫秒后返回输出,但仅在播放或停止时返回。我的代码中是否缺少某些内容。(我是 Expo 和 Expo AV 的新手)。请参阅下面用于加载和播放音频文件的代码

if(!sound._loaded)
    await sound.loadAsync(
      require('./assets/Audio.mp3'),
      initialStatus,
    );
    
    sound.setStatusAsync({progressUpdateIntervalMillis: 200});
    sound.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate);

    await sound.playAsync();
    console.log(sound);
  }

  const initialStatus = (status) => {
    console.log("initialStatus");
    console.log(status);
  }

  let onPlaybackStatusUpdate = async (playbackStatus) => {
    //if(!isNaN(playbackStatus.durationMillis))
    //setTotalLength(playbackStatus.durationMillis/1000);
    //setCurrentPos(playbackStatus.positionMillis.toString().split(".")[0]/1000);
    //setIsPlaying(playbackStatus.isPlaying);
    console.log("onPlaybackStatusUpdate");
    console.log(playbackStatus);
  }
4

1 回答 1

1

对于声音实例,创建一个ref变量并使用它来触发暂停/播放以及加载。

在这里工作实施

像这样,

const sound = React.useRef(new Audio.Sound());

对于加载音频,

const result = await sound.current.loadAsync(require('./test.mp3'), {}, true);
if (result.isLoaded === false) {
  console.log('Error in Loading Audio');
} else {
  // Audio is loaded do whatever needed
}

暂停

const result = await sound.current.getStatusAsync();
if (result.isLoaded) {
  if (result.isPlaying === true) {
    // sound.current.pauseAsync();
    // Pause Here
  }
}

为......而玩,

const result = await sound.current.getStatusAsync();
if (result.isLoaded) {
  if (result.isPlaying === false) {
    // sound.current.playAsync();
    // play Here
  }
}
于 2021-05-20T13:51:52.007 回答