react-native-track-player icon indicating copy to clipboard operation
react-native-track-player copied to clipboard

IOS. Buffering takes too long or ends in an error

Open yeezussniper opened this issue 7 months ago • 1 comments

The behaviour is random, I can successfully and quickly turn on 3-5 tracks and everything hangs, but it can also hang immediately on the first track. The behaviour is not clear to me, it works very randomly

I know this isn't a new problem, I've been seeing threads since ~20 years. But I don't see a normal explanation or solution

My init player code

async function initPlayer() {
    let isPlayerInitialized = false;

    if (!isPlayerInitialized) {
        try {
            await TrackPlayer.setupPlayer({
                autoHandleInterruptions: true,
                waitForBuffer: true
            }).then(async () => {
                await TrackPlayer.updateOptions({
                    capabilities: [
                        Capability.Play,
                        Capability.Pause,
                        Capability.JumpBackward,
                        Capability.JumpForward,
                        Capability.SeekTo
                    ]
                });
            });

            TrackPlayer.addEventListener(Event.PlaybackState, (e) => {
                console.log('PlaybackState', e, dayjs().format('HH:MM:SS YYYY-MM-DD'))
            })

            isPlayerInitialized = true;
            console.log('Player initial')
        } catch (e) {
            isPlayerInitialized = false;
            console.log(e)
        }
    }
}

initPlayer()

How I switch tracks

    const activeTrack = await TrackPlayer.getActiveTrack()

    if (activeTrack) {
        await TrackPlayer.stop()
        await TrackPlayer.reset()
        console.warn('Old track has been reset');
    }

    await TrackPlayer.add({
        url: episode.recordUrl,
        title: episode.title,
        artist: episode.album.author,
        album: episode.album.title,
        artwork: process.env.EXPO_PUBLIC_API_URL + episode.image,
        id: episode.id,
        contentType: 'audio/mpeg'
    }).then(async () => {
        console.warn('New track has been added');
        await TrackPlayer.play()
    });

Logs


 LOG  PlaybackState {"state": "loading"} 07:21:15 2024-07-20
 LOG  PlaybackState {"state": "buffering"} 07:21:15 2024-07-20
 LOG  PlaybackState {"state": "ready"} 07:21:32 2024-07-20
 LOG  PlaybackState {"state": "playing"} 07:21:32 2024-07-20

or

 LOG  PlaybackState {"state": "loading"} 07:22:02 2024-07-20
 LOG  PlaybackState {"state": "buffering"} 07:22:02 2024-07-20
 LOG  PlaybackState {"error": {"code": "ios_failed_to_load_resource", "message": "Failed to load resource"}, "state": "error"} 07:24:02 2024-07-20
 ERROR  {"error": "The operation couldn’t be completed. (SwiftAudioEx.AudioPlayerError.PlaybackError error 1.)"}


Environment Info: Paste the results of npx react-native info Paste the exact react-native-track-player version you are using Real device? Or simulator? What OS are you running?

Packages version: "react-native": "0.74.1", "react-native-track-player": "^4.1.1" IOS Simulator 17.5 Mac OS

yeezussniper avatar Jul 20 '24 12:07 yeezussniper