Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 793
Adarkuccio Avatar asked Sep 12 '13 11:09

Adarkuccio


People also ask

How do I stop music from pausing when I open apps?

Turn off Power saving mode. Apps may be closing in the background depending on the selected Power mode. Open Settings, tap Battery and device care, and then tap Battery. Turn off Power saving by tapping the switch.

How do I make my iPhone stop defaulting to Apple Music?

Tap the Autoplay icon to disable it. The Autoplay icon is the one that looks like an infinity symbol. If done correctly, the Autoplay playlist should immediately disappear, and the Apple Music will now stop automatically playing music.


2 Answers

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

like image 138
Qaiser Abbas Avatar answered Dec 07 '22 19:12

Qaiser Abbas


Swift:

Before playing enter this line:

let audioSession = AVAudioSession.sharedInstance()
if audioSession.otherAudioPlaying {
    _ = try? audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
}
like image 42
Esqarrouth Avatar answered Dec 07 '22 18:12

Esqarrouth