如何检索下载的文件以播放反应原生



我正在使用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);
        });
    }

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

这对我有用,但使用反应原生视频

<Video
    source={{ uri: "file://" + RNFS.DocumentDirectoryPath + "/Claymore-OP01-NCBD.mp4" }} />

所以基本上,在路径之前添加架构:

file://somepath.mp3

相关内容

  • 没有找到相关文章

最新更新