如何检索下载的文件以播放React-native

时间:2018-04-04 15:00:00

标签: react-native download mp3 react-native-fetch-blob react-native-fs

我正在使用react-native-fetch-blob下载文件。 文件位于RNFS.DocumentDirectoryPath + fileExtension; eq:RNFS.DocumentDirectoryPath /myMusic.mp3;

下载的文件位于

/data/user/0/com.myproject/files/myMusic.mp3

并根据react-native-sound,它要求将文件放在原始文件夹下。 当我使用react-native-fs跟随下面的代码,它正在播放!!!!

downloadFileAndPlay()
    {
        console.log('Download');

        const downloadDest = RNFS.DocumentDirectoryPath + '/sample.mp3';
        const ret = RNFS.downloadFile(
            {
                fromUrl: 'http://www.julien-gustin.be/files/sample.mp3',
                toFile: downloadDest
            }
        );
        ret.promise.then(res => {
            console.log(res);
            // Load the sound file 'whoosh.mp3' from the app bundle
            // See notes below about preloading sounds within initialization code below.
            console.log(downloadDest);
            var whoosh = new Sound(downloadDest, '', (error) => {
                if (error) {
                    console.log('failed to load the sound', error);
                    return;
                }
                // loaded successfully
                console.log('duration in seconds: ' + whoosh.getDuration() + 'number of channels: ' + whoosh.getNumberOfChannels());

                if(whoosh.isLoaded()){
                    console.log('LOADED');
                }else{
                    console.log('NOT LOADED');
                }

                // Play the sound with an onEnd callback
                whoosh.play((success) => {
                    if (success) {
                        console.log('successfully finished playing');
                    } else {
                        console.log('playback failed due to audio decoding errors');
                    }
                });
            });

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

在这两个地方我都使用相同的目录路径。但在我们的情况下它不玩.. 如何从其他组件调用此文件时,如何检索下载的文件并播放?

0 个答案:

没有答案