Stop background sound if Sound id played from react native app - react-native

I have video player in my app, which is working fine. But there is one problem when I play the video , if there is another sound playing in the background , it keeps on playing. The ideal scenario should be that background sound should stop playing and when I pause video then background sound should start playing again. I don't know how to handle such scenarios. Any help would be appreciated

On iOS you can use AVAudioSessionCategoryAmbient to set how should sounds mix. Here is the list for possible options. Here and Here are the methods for react-native-sound for how you can set.
Sound.setCategory(value, mixWithOthers)
value {string} Sets AVAudioSession category, which allows playing
sound in background, stop sound playback when phone is locked, etc.
Parameter options: "Ambient", "SoloAmbient", "Playback", "Record",
"PlayAndRecord", "AudioProcessing", "MultiRoute".
Sound.setMode(value)
value {string} Sets AVAudioSession mode, which works in conjunction
with the category to determine audio mixing behavior. Parameter
options: "Default", "VoiceChat", "VideoChat", "GameChat",
"VideoRecording", "Measurement", "MoviePlayback", "SpokenAudio".
This should be called in conjunction with Sound.setCategory.

Related

play audio in background on intervals iOS

So I am working on an app that requires different audio files to be played using an NSTimer every X amount of time. Yes, it needs to play the audio in the background as well, but I have had inconsistent results trying to do so. I have searched the web thoroughly to find a solution to this problem, but I have had no luck.
I have enabled background modes and checked off item 0(audio) and made sure it was in my info.plist. I have set the "App does not run in background" key to NO in the info.plist. In the viewDidLoad method of the VC, I have set the AVAudioCategory to categoryPlayback and set the AVAudioSession to active. I have also moved these lines of code around in different places of the app to look for different results, and the best results I have gotten is that the app will continue to run in the background sometimes and at other times it will suspend.
Through all of my searching, I have seen forums explaining how to play audio in the background. However, I have not found any solutions to playing short audio files in the background intermittently on different scheduled intervals.
I know of at least one app in the App Store that is able to do this, and they use the ducking feature of AVFoundation to duck audio playing in the background(Im not sure if other frameworks are able to duck background audio, such as AudioToolbox, but I would like to have ducking enabled for my app).
If anyone knows of any legal solution for my problem, your assistance would be greatly appreciated.

Playing a muted video without interrupting background music

My design calls for a video playing in the background of my login screen, exactly like 6snap has.
I would like to avoid the default behavior of stopping the user's music when the video starts to play. My video does not have sound.
I'm using:
<MediaElement Source="MyVideo.mp4" />
I tried setting IsMuted=true which didn't help. Does anyone have an idea how 6snap managed it?
Edit: currently trying the animated gif route. Using the ImageTools 3rd party library and having converted my MP4, it works fine. My 9 second 640x1136 3MB video became a 41MB GIF, so I have to reduce the quality drastically. Still trying to find a better way if possible.
You won't be able to do that with Background Audio and MediaElement, hence as MSDN says:
When a MediaElement control plays audio or video content, any background sounds or media already playing are halted. The app launches the playback experience when the user taps the control. Only one MediaElement control can operate at a time.
It's no matter you have no sound - when you start to play all background sounds/media are halted.
I'm not sure how the App you have mentioned achieved that, but maybe you can try with DirectX/XNA - thought I've not tried this and don't know if that would help.

iOS how to don't stop music when the app start launching

I've noticed that when my app start, the music I'm listening is automatically stopped, and I've noticed that when I start some other apps, the music just continue... this means that I don't know how to manage the actual playing music in the device to let it plays or stop.
I'm developing a game with obj-c and cocos2d btw, I've searched but sadly I've found nothing... so here's my question, how can I let the music I'm listening with my device continue to play even if I start the app ?
edit: I'm using SimpleAudioEngine to start a background music and some sound effects in my app
Place this line in your application:didFinishLaunchingWithOptions: method of your AppDelegate or in general before using the audio player.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
According to the documentation, the AVAudioSessionCategoryAmbient category is
for an app in which sound playback is nonprimary—that is, your app can
be used successfully with the sound turned off.
This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays over iPod audio. When you
use this category, audio from other apps mixes with your audio. Your
audio is silenced by screen locking and by the Silent switch (called
the Ring/Silent switch on iPhone).
please import AVFoundation/AVFoundation.h
Swift:
Before playing enter this line:
let audioSession = AVAudioSession.sharedInstance()
if audioSession.otherAudioPlaying {
_ = try? audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
}
For people looking to solve this within react native (iOS)
My problem was quite the opposite where some of our muted videos with no music was causing background sound to be paused from other apps for no reason.
To fix this, also pass ignoreSilentSwitch="obey" prop alongside disableFocus and muted props through your Video component.
According to the docs:
You can activate the audio session at any time after setting its category, but it’s generally preferable to defer this call until your app begins audio playback. Deferring the call ensures that you won’t prematurely interrupt any other background audio that may be in progress.
Be sure not to be starting the audio session directly via its setActive method or indirectly via AVAudioEngine or other playback engine(s) immediately after the app launches. You should set only the category at this stage if you don't want your app to interrupt others on launch. If you're using AVAudioEngine, defer setting up the engine or connecting anything to the engine's mainMixerNode or outputNode until you actually need to start playing something.
When your app starts playing music, it will pause any audio currently playing. For obvious reasons. If you want your music to carry on playing when the app launches then don't let the app play any music.

How can I prevent my application (using AVPlayer) is muted when the ringtone is muted?

The topic may sound strange, but please keep reading on.
I am using AVPlayer to play some sound from the iPod library and everything works fine when using it with headphones plugged in, but when I want to use the app to play from the built in speakers there is no sound output. (The time counter and everything else in the application is still working like there would be sound output. Like it is just muted.)
I checked the volume and the mute lock and then found out that the ringtone was muted. But ringtone muting doesn't affect other applications like the iPod app itself or the Spotify music app.
How can I prevent my application (using AVPlayer) is muted when the ringtone is muted and behave like other music player apps?
You should set your app's audio session to AVAudioSessionCategoryPlayback. From the docs:
Use this category for an
application whose audio playback is of primary importance. Your audio
plays even with the screen locked and with the Ring/Silent switch set
to silent.

OverrideCategoryDefaultToSpeaker not responding with iPod Touch

A little background:
I am working on an iOS app which does audio recording and makes use of the audio session category AVAudioSessionCategoryPlayAndRecord. The app also plays sound effects which I want to play through the device's main speaker (while the user is not recording). In order stop output audio from playing through the main speakers while recording I use the following code snippet:
UInt32 defaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (defaultRoute),
&defaultRoute);
When the user starts recording I run the same code again with a defaultRoute of 0 which causes output audio to stop being played through the speakers and therefore not included in the recording. This method has been working fine on the iPhone.
The Issue:
When I run the same code on an iPod Touch (4th generation), the kAudioSessionProperty_OverrideCategoryDefaultToSpeaker property does not seem to have any affect. The result is that while a user is recording audio on their iPod Touch, sound effects will still play through the speaker and are included in the recording.
Does anyone know why this is happening, or how to fix it? I want to the behaviour on the iPod Touch to match the behaviour on the iPhone: output audio does not play through the main speakers while recording.
Note:
The closest to a solution that I have gotten is detecting if the device is an iPod Touch and if it is, mute system audio when recording starts using the following code snippet:
[MPMusicPlayerController iPodMusicPlayer].volume = 0.0;
This was not a desirable solution as when this code is executed, the volume overlay view pops up showing that the volume was turned down. This adds confusion to the UI and is not a good enough solution.
Any help would be appreciated!