react-native-track-player的持续时间始终为0

时间:2018-07-12 11:24:54

标签: react-native audio async-await react-native-android

“反应”:“ 16.3.1”, “ react-native”:“ 0.55.4”, “ react-native-track-player”:“ ^ 0.2.4”, “平台”:“ Android” “ OS”:“ 7.0”

我想获取音频MP3从网站上线的总时间。我正在使用react-native-track-player。首先,我只添加了曲目详细信息,然后设置了背景功能,然后我尝试获得我刚刚设置的音频持续时间。 这是我的代码:

componentWillMount() {
    TrackPlayer.setupPlayer().then(
      async () =>
        await TrackPlayer.add({
          id: "123",
          url: this.state.Url,
          title: "Track Title",
          artist: "Track Artist",
          artwork:
            "https://socialecologies.files.wordpress.com/2015/10/a_monk.jpg"
        }).then(() =>{
          TrackPlayer.updateOptions({
            capabilities: [
              TrackPlayer.CAPABILITY_PLAY,
              TrackPlayer.CAPABILITY_PAUSE,
              TrackPlayer.CAPABILITY_STOP
            ],
            compactCapabilities: [
              TrackPlayer.CAPABILITY_PLAY,
              TrackPlayer.CAPABILITY_PAUSE,
              TrackPlayer.CAPABILITY_STOP
            ]
          });
          this._GetTrackDuration()
        }
        )
    )
  }

这是我获得音频功能的持续时间:

_GetTrackDuration = async () => {
  let GetDuration = await TrackPlayer.getDuration();
   alert(GetDuration);
   this.setState({
   duration: GetDuration
   });
   };

我总是收到Trackplayer.getDuration()==0。我在做什么错??

更新: 当我在 Debug模式中运行它时,它会准确显示Trackplayer.getDuration()所期望的内容,但是这次音频播放了2次。在没有 Debug Mode 的情况下,音频播放1次。 我的游戏代码:

componentDidMount() {
    this.play();
  }

我的播放功能:

play = () => {
    this.setState({ playState: "playing" });
    TrackPlayer.play();
  };

0 个答案:

没有答案
相关问题