我正在React Native expo中实现媒体播放器。我已经设计了我的播放器、播放歌曲、下一首和上一首歌曲功能,但我在获取歌曲的持续时间时遇到了问题,以便能够使用进度条显示歌曲的进度。
我已经浏览了 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;
};
提前谢谢你
我有用。但它是在Sound
类组件中使用的。
CurrentDuration = async () => {
const Mill = await this.soundObject.getStatusAsync();
let p = Mill.positionMillis;
return {curr:p};
};
我不确定您是否仍然感兴趣,但是,请按照一种方法来获取持续时间。
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 轨道:
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,
}
还有许多其他信息可用,如果您需要特定信息,请告诉我。
我希望这有帮助!
干杯