Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Ability to resume music, that was playing by other app, interrupted by rn-sound #824

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

polvere94
Copy link

@polvere94 polvere94 commented Apr 4, 2023

This PR adds the ability to resume playing music interrupted by react-native-sound playing an audio when you want to use playback category with mixWithOthers set to false.
This allows to obtain the behavior like the one adopted by Telegram or Whatsapp for voice messages:
Music interrupted by the playback of a voice message is resumed when rn-sound stops playing.

To achieve this, I added the AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation option to the function setActive according to the iOS doc Apple doc

Usage:
after playing an audio, call the setActive(false, true) function where the last parameter (true) indicates whether you want to notify other apps of the sound deactivation and thus have the app that was interrupted resume playback.
The default value is false to not alter the behavior of previous versions

Example:

// Enable playback in silence mode
Sound.setCategory('Playback');

var whoosh = new Sound('whoosh.mp3', Sound.MAIN_BUNDLE, (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());

  // Play the sound with an onEnd callback
  whoosh.play((success) => {
    // do something
    Sound.setActive(false, true);
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants