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

5

17

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

Gunstock answered 12/9, 2013 at 11:5 Comment(0)
D
22

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

Deloresdeloria answered 5/5, 2014 at 4:42 Comment(3)
I open my app , music from background app stopped. below solutions is not working for me [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; guys any suggestion help? above solution is working when we do not use any AVAudioPlayer or AudioServicesCreateSystemSoundIDGelya
I imported but it says "use of undeclared identifier 'AVAudioSession'"Oxycephaly
Ok, you have to import it before this line #if RCT_NEW_ARCH_ENABLED then it finds itOxycephaly
G
10

Swift:

Before playing enter this line:

let audioSession = AVAudioSession.sharedInstance()
if audioSession.otherAudioPlaying {
    _ = try? audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
}
Gony answered 26/4, 2017 at 15:3 Comment(1)
I am setting this as suggested but I don't want the mixing sound, I am expecting my ios app should trump the apple music/other music audio, if audio was initiated in my app but not just by opening the app. any suggestion pleaseHigbee
S
4

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.

Staton answered 15/1, 2020 at 18:14 Comment(2)
this is correct answer for react nativeDodson
how to make it work with ignoreSilentSwitch="ignore"Oxycephaly
T
1

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.

Tieratierce answered 3/4, 2019 at 23:27 Comment(0)
E
-9

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.

Enarthrosis answered 12/9, 2013 at 15:33 Comment(2)
the music stop even if my app has music turned off, and btw I'm pretty sure that some app let the device music playing even if their app have music playing (so yes you will listen both music at the same time)Gunstock
Yes, you can play music with while playing Candy Crush Saga and you can hear also its sounds.Strake

© 2022 - 2024 — McMap. All rights reserved.