doublesymmetry / react-native-track-player

A fully fledged audio module created for music apps. Provides audio playback, external media controls, background mode and more!
https://rntp.dev/
Apache License 2.0
3.18k stars 981 forks source link

Interruption does not start back after the Track Player Reset #2209

Closed cbaser closed 4 months ago

cbaser commented 7 months ago

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"))
            })
        })
    })
}
lovegaoshi commented 7 months ago

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

cbaser commented 7 months ago

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

lovegaoshi commented 7 months ago

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

github-actions[bot] commented 4 months ago

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] commented 4 months ago

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