Objective-c – iOS how to don’t stop music when the app start launching

objective cplayback

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

Best Answer

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

Related Topic