如何在React Native中预加载音频文件?

时间:2019-10-03 11:51:53

标签: javascript reactjs react-native expo

我现在正在开发一个媒体播放器,但我很想找到一种解决方案,可以在其中预加载前4个音频文件,当我进入下一个音频文件时,具有预加载功能将再次运行。我正在为每个指向s3存储桶的URL而不是本地使用URL数组。我还想提到我正在使用expo-av软件包

目前,加载看起来像这样

componentDidMount() {
        Audio.setAudioModeAsync({
            allowsRecordingIOS: false,
            staysActiveInBackground: false,
            interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
            playsInSilentModeIOS: true,
            shouldDuckAndroid: true,
            interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
            playThroughEarpieceAndroid: false
        });
    }

    async _loadNewPlaybackInstance(playing) {
        if (this.playbackInstance != null) {
            await this.playbackInstance.unloadAsync();
            this.playbackInstance = null;
        }

        const source = { uri: PLAYLIST[this.index].url };
        const initialStatus = {
            shouldPlay: playing,
            rate: this.state.rate,
            shouldCorrectPitch: this.state.shouldCorrectPitch,
            volume: this.state.volume,
        };

        const { sound, status } = await Audio.Sound.createAsync(
            source,
            initialStatus,
            this._onPlaybackStatusUpdate
        );
        this.playbackInstance = sound;

        if (status) {
            this._onPlayPausePressed()
        }

        this._updateScreenForLoading(false);
    }

0 个答案:

没有答案