在React Native博览会中构建媒体播放器并获取歌曲持续时间

时间:2019-04-16 04:40:02

标签: react-native audio expo

我正在React Native博览会中实现媒体播放器。我已经设计了播放器,播放歌曲,下一首和上一首歌曲的功能,但是在获取歌曲的持续时间时遇到了问题,以便能够使用进度条显示歌曲的进度。

我经历了https://docs.expo.io/versions/latest/sdk/audio/,但没有找到有关如何获取文件持续时间或如何获取任何对进度条实施有帮助的信息。任何想法都非常欢迎。

我正在尝试的代码:

// Create new object from Expo.Audio.Sound
   this.soundObject = new Audio.Sound();
   await this.soundObject.loadAsync(url);
   await this.soundObject.playAsync();


   getCurrentItemArtistName = ()=> {
        return this.list[this.index].artistname;
    };

   getCurrentItemCover = ()=> {
        return this.list[this.index].cover;
    };

先谢谢您

2 个答案:

答案 0 :(得分:0)

这对我有用。但这是在Sound类组件中使用的。

CurrentDuration = async () => {
        const Mill = await this.soundObject.getStatusAsync();
        let p = Mill.positionMillis;
    return {curr:p};
  };

答案 1 :(得分:0)

我不确定您是否仍然感兴趣,但是请遵循获取持续时间的方法。

const sound = new Audio.Sound();

try {

   const track = await sound.loadAsync(require('../../models/smw_bonus_game_end.mp3'));

   console.log(track.durationMillis); // Prints the duration in milliseconds

  } catch (err) {
   console.log(err);
  };

加载文件后,您可以获取持续时间


其他信息可以在对象中找到。使用下面的相同代码,只需尝试仅打印const track

console.log(track);

它将打印如下对象:

 Object {
  "didJustFinish": false,
  "durationMillis": 3108,
  "hasJustBeenInterrupted": false,
  "isBuffering": false,
  "isLoaded": true,
  "isLooping": false,
  "isMuted": false,
  "isPlaying": false,
  "pitchCorrectionQuality": "Varispeed",
  "playableDurationMillis": 3108,
  "positionMillis": 0,
  "progressUpdateIntervalMillis": 500,
  "rate": 1,
  "shouldCorrectPitch": false,
  "shouldPlay": false,
  "uri": "THE URI OF THE FILE IN YOUR DEVICE",
  "volume": 1,
}

还有许多其他信息,如果您需要特定的信息,请告诉我。

我希望这会有所帮助!

干杯