React-native-track-player can not play local files which is fetched by rn-fetch-blob

2.6k Views Asked by At

After using rn-fetch-blob to get .mp3 file, i typed console.log(filePathIos) and got the path like this: "/var/mobile/Containers/Data/Application/80D7496B-862A-44D6-9D3A-F0EF31B565CF/Documents/xxx.mp3"

My code for playing track:

let filePathIos = `${fs.dirs.DocumentDir}/${fileName}`;
const thisSong = props.songs[0]
const track = {
        id: thisSong.id.toString(),
        url: filePathIos,
        title: thisSong.title,
        artist: "xxx"
}
const togglePlayback = async () => {
        setAudioStatus(!AudioStatus)
        const currentTrack = await TrackPlayer.getCurrentTrack();
        if (currentTrack == null) {
            await TrackPlayer.add(track);
            await TrackPlayer.play();
        } else {
            if (AudioStatus) {
                await TrackPlayer.play();
            } else {
                await TrackPlayer.pause();
            }
        }
    }

The problem is: The local file is downloaded sucessfully and i can open it locally. But the above code dont work. when i try to press toggle button, xcode shows some logs as: "Starting/Resuming playback" but the song is still not played.

If i try to change a litte in the code abve ( change the url to an online music url) like this:

const track = {
        id: thisSong.id.toString(),
        url: 'https://audio-previews.elements.envatousercontent.com/files/103682271/preview.mp3',
        title: thisSong.title,
        artist: "xxx"
}

then the app works like a charm and it can play the song.

Im using these packages to build app, and for some specific reasons, i cannot upgrade/downgrade these packages:

Things i tried with no luck:

  • changes url from url: filePathIos to url: 'file://' + encodeURIComponent(filePathIos)

Thank you for any ideas!

1

There are 1 best solutions below

3
On

Well, found it. The problem is that version 1.2.3 has an error on Track.swift that is fixed on the Dev branch:

func getSourceUrl() -> String {
        return url.isLocal ? url.value.path : url.value.absoluteString
}

in version 1.2.3 is:

func getSourceUrl() -> String {
        return url.value.absoluteString
}

This prevents local files to be played.

You can use the "Dev" branch as a workaround. I will fill a bug report.