Preventing my app's audio from interfering with other background music - ios

I think what I'm looking for is the combination of existing settings that I can't quite put together in the right order. I have an app that plays sound clips using AVAudioPlayer. There is a start/stop button.
I would like any other apps' audio to continue until my app actually needs to play something.
When my app is launched with some iTunes music playing, that music continues correctly. When I press play in my app, the other audio correctly stops, and my app plays. I stop my player, switch back to the music app, and press play again. But now when I switch back to my own app, the music stops immediately.
I would like it to again wait until it needs to play something before killing the music app.
My best guess is that once I've used the player, it is in some kind of 'I need audio' state, and remains in that state. How do I resign this state?

Related

How to play youtube video while mixing with background music?

I have an app that is used in the gym. The user may want to watch a video demonstrating an exercise without stopping the music they are playing through Spotify or some other app.
I used to do with with a UIWebView, and mix the audio in AVAudioSession, but I can't do this with WKWebView. WKWebView takes over the audio session and stops whatever else is playing, then it doesn't restart the audio it stopped.
My question is, what other ways can I play the Youtube video in my app without stopping the background music? or if it must stop, at least be able to restart the background music.
Music stopping at the gym and being forced to go out of the app to restart it is terrible user experience.
Also, please note that going back to UIWebView is not an option, as Apple is deprecating this class.

Resume playing music after alarm

My application have permanent music inside that should play when it opened. Problem appears when alarm rang. It automatically stops app music, but it's not resumed after. Anyone know how can I handle this?

How to stop MPMusicPlayerController from playing when application terminate by user?

I have the following situation in my application:
I have a music system in my application and I used MPMusicPlayerController to play music; every thing is working fine until now.
My problem: When a user starts playing music in my application and after some time it terminates, music cannot be stopped because I'm using the [MPMusicPlayerController systemMusicPlayer] object. I know there is another option which is applicationMusicPlayer, but it stops playing music in background, which doesn't satisfy my requirements.
How can I stop the music from playing when the application is terminated by user?
I have some code that attempts to stop it in applicationWillTerminate: but it only works in some situations:
If I press home button twice and terminate the app from the multitasking UI, then the app can stop the music player.
If I press the home button once and then go to the home screen, and after that I press the home button twice and terminate the application then it can not stop my music player.
I tried to put a breakpoint in applicationWillTerminate: but in the second example (from above), the application crashed and did not execute my code, unlike in the first situation.
UPDAT
And I know that when I use MPMusicPlayerController background mode is not required because it starts music in the native music player.
Any help would be appreciated.
Unfortunately your only option based on your requirements is to play the audio using your own app, AVAudioPlayer for example and set the audio background mode requirement.
As there is no way of receiving a notification when your app is terminated by the OS or by the user, you have no way around it, you will not be able to stop the system or application player from your process.
Registering for NSUncaughtExceptionHandler wont catch SIGKILL or SIGSTOP either so you cannot rely on the system music player.
This shouldnt be a problem though as its easy to setup audio playback in your app. If you are using the system player so that the user can play their own music, you should use a picker and access their music library so that they can choose the music.
In this case:
If I press the home button once and then go to the home screen, and after that I press the home button twice and terminate the application then it can not stop my music player.
You have to register application in the background mode.
fileprivate var backgroundTask: UIBackgroundTaskIdentifier = .invalid
func registerBackgroundTask() {
self.backgroundTask = UIApplication.shared.beginBackgroundTask {
//TODO
}
}
Call it in AppDelegate
Hope to help you

Music doesn't play when some apps are open in the background - ios cocos2d

I have encountered an issue with my ios cocos2d app, that while certain apps are open in the background, my app's music doesn't play.
After I terminate those apps from the background, the music works again.
I use the following mode for CDAudioManager:
[[CDAudioManager sharedManager] setMode:kAMM_FxPlusMusicIfNoOtherAudio];
I use this in order to allow the user to play background music from his music player.
When I change this mode to kAMM_FxPlusMusic for example, my app's music is played even if those apps are open - which is great. However, the music from the music player in this case is paused, which is not what I want.
The main question is:
Why do some apps cause my app's music to not play, and can I do something about it while still allowing the user to play music from his music player?

How to continue playing a youtube video at background when switch from my app back to home screen

Seems the default action for iOS 6 is to stop the video from playing after I switch to HOME screen. Then if I want to listen to the audio I have to go back to the app and press the 'Play' button again. I am using youtube API.
How can I continue playing the video when the application is in UIApplicationStateInactive and UIApplicationStateBackground. I just want to be able to listen to the audio even if I am on HOME screen or the screen is locked. Should I use a background thread or restart playing the video at the previous position as it was before turning the app inactive?
Take a look at the app 'Jasmine'. They managed to do something similar to what you are seeking. However, UIApplicationStateInactive/UIApplicationStateBackground will cause the video to pause. Double tapping the home button and then pressing the audio play button will cause the video's audio to resume without keeping the app active.
Take a look at the new iOS 7. You may have some luck there.
Inter-App Audio
Now your apps can make beautiful music together. With Inter-App Audio,
apps can register their audio streams to share with other apps. For
example, a series of apps could publish audio streams of instrument
tracks while another uses the combination of these streams to compose
a song. Inter-App Audio also provides for MIDI control of audio
rendering, remotely launching other registered Inter-App Audio apps
and more.

Resources