2

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);
        });
    }

在这两个地方我都使用相同的目录路径。但在我们的例子中,它没有播放..当我从另一个组件调用这个文件时,如何检索下载的文件并播放?

4

1 回答 1

0

这对我有用,但使用react-native-video

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

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

文件://somepath.mp3

于 2022-02-14T06:38:25.887 回答