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

Interruption does not start back after the Track Player Reset

Open cbaser opened this issue 2 years ago • 3 comments

Hello, I have two different problems.

My scenario is the following -> user plays an audio from Spotify, Youtube etc. and then starts the app and app will trigger the sounds automatically.

  1. In iOS, Ducking works but after the duck the volume of the Spotify is not the same as before ( it has less volume)
  2. In Android, interruption works but does not start the Spotify Back.

My Implementation:

service.js

import TrackPlayer from 'react-native-track-player';

module.exports = async function () {

    TrackPlayer.addEventListener('remote-play', () => TrackPlayer.play());
    TrackPlayer.addEventListener('remote-pause', () => TrackPlayer.pause());

};

App.js

 await TrackPlayer.setupPlayer({

            autoHandleInterruptions:true,
            iosCategoryOptions: [
                IOSCategoryOptions.DuckOthers
            ]
        });
        await TrackPlayer.updateOptions({
            capabilities: [
                Capability.Play,
                Capability.Pause
            ],
            android: {
                alwaysPauseOnInterruption: true,
                // This is the default behavior
                appKilledPlaybackBehavior: AppKilledPlaybackBehavior.ContinuePlayback,
                stopForegroundGracePeriod: 2
            },
            icon: require("../assets/img/logo_app_rounded.png")
        });

Component

const playAudio= (audioId, lat,lng) =>{
    const url = BASE_URL + "/audios/" + audioId
    axios.get(url).then((response) => {
        let audioData =response.data
        TrackPlayer.add([{
            url: audioData.url,
            title: audioData.name,
            artist: "Artist"
        }]).then(() => {
            TrackPlayer.play().then(() =>{
                 console.log("Playing")
            })
            TrackPlayer.addEventListener(Event.PlaybackQueueEnded, () => {
                TrackPlayer.reset().then(()=> console.log("Track Player Reset"))
            })
        })
    })
}

cbaser avatar Nov 29 '23 13:11 cbaser

for ios you are not alone. though im not aware of a solution. for android i believe its not supposed to bring other media playback back - RNTP is a music player, not a short video player. when using system managed audio focus it requests AUDIOFOCUS_GAIN not AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE so audiofocus is not granted back to the previous owner. This is also true for any other music players where pausing one doesnt automatically restore playback from another. youre always welcome to fork and change the audiofocus gain type of course.

https://developer.android.com/media/optimize/audio-focus#audio-focus-change

lovegaoshi avatar Nov 29 '23 16:11 lovegaoshi

@lovegaoshi thank you! Do you know which part of the code should I change in order to get this functionality?

cbaser avatar Nov 30 '23 09:11 cbaser

u can try replacing AUDIOFOCUS_GAIN to AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE in https://github.com/doublesymmetry/KotlinAudio and see if it works

lovegaoshi avatar Nov 30 '23 16:11 lovegaoshi

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Feb 29 '24 01:02 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Mar 08 '24 01:03 github-actions[bot]