首页 > 解决方案 > 如何在本机反应中预加载音频文件?

问题描述

我现在正在开发一个媒体播放器,但我很想找到一个解决方案,我可以预加载前 4 个音频文件,当我去下一个时,预加载的功能将再次运行。我正在使用一个带有 url 路径的数组,每个数组都指向非本地的 s3 存储桶。我还想提一下我正在使用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);
    }

标签: javascriptreactjsreact-nativeexpo

解决方案


推荐阅读