Using the AVAudioSession class, it is possible to play audio in the background, but I was wondering if it is possible to play audio in the background and having the built-in music player playing at the same time. Based on the documentation, this seems to be impossible.
Is there a configuration that I am overlooking or a reliable workaround?
Related
I have an app that records a video/audio like Instagram stories with avassetwriter and simultaneously plays an audio background music file with avaudioplayer. It both records and plays great.
The problem I am noticing though, is when I play back the recording, the background music is extremely loud, and I'm assuming it's because the mic and the bottom-of-the-phone default speaker, through which the audio background music is playing out of, are situated right next to each other - even though when I record it sounds like it's not that loud.
Is there any possible way or workaround that results in allowing the background music to play while recording but to not have it actually be recorded? I'm stumped on what my options are, and am desperately hoping to see if anyone else had any ideas/thoughts as I couldn't find any existing content addressing this issue.
I am building a iOS app with Ionic and Capacitor. I use AVFoundation inside a Capacitor plugin which provides a public method to play an audio file accessible at a remote URL. The basics work, the audio file is successfully played. Now I want to correctly handle interruptions and background audio. By background audio I mean start playing an audio file while the app is in the background and not continue playing it in the background when it was started in foreground before. To summarize, the app should behave the following way:
If another app (e.g. Spotify) is playing audio and my app wants to play audio, the other app should be interrupted and my app's audio should start playing without mixing.
If my app's audio is finished playing, the other app's audio should resume.
My app must be able to start playing audio without mixing when it is in the background.
I want to show a play/pause button and the audio title on the lockscreen and in the notification center.
I added the background mode audio capability. I use AVPlayer, AVAudioSession, MPNowPlayingInfoCenter and MPRemoteCommandCenter, followed best practices, especially regarding activation and deactivation of an audio session and interruption handling. I followed this question and this question. I tried several solutions, but never managed to achieve all of the above goals. What I found out so far is the following:
Goal 1. above is easy to achieve. Simply activate an audio session without mixing and start playing the audio.
After the audio of my app is finished, I deactivate the audio session. Unfortunately, I can only achieve goal 2. above if I don't use remote commands nor notification info. This means, my app then has no audio control UI on the lockscreen nor in the notification center. Somehow the configurations of MPNowPlayingInfoCenter and MPRemoteCommandCenter seem to affect the interruption behavior. As far as I could see, Apple documentation doesn't say anything about this.
I only manage to start playing audio in the background when setting the option .mixWithOthers. Apple documentation doesn't say anything about this, either.
Any help would be greatly appreciated.
Rony
Hello i am trying to play background music with MPMusicPlayerController's applicationMusicPlayer in iOS.
I have also added Required background modes in Plist.
But it doesn't play background music.
How can i play background music with MPMusicPlayerController?
Thanks for your reading. :)
The applicationMusicPlayer does not support background music. Use MPMusicPlayerController's iPodMusicPlayer instead. It shares the state with the built-in iPod player and music will continue to play when your app enters the background.
Bryan is right about applicationMusicPlayer not playing music in background, but there is another solution:
You can use AVAudioPlayer to play either streamed or local(bundle) audio. It even allows you to play multiple audio file at the same time having an AVAudioPlayer instance for each.
Here's the documentation:
http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
In my application I have made [audioSession setCategory:AVAudioSessionCategoryPlayback] to play audio in back ground and also to mute other audio play backs while playing song in my application. I am playing the audio using AVPlayer and also I have UIWebView for playing video in same Application. The issue arises when I play both video in web view and audio using AVPlayer. The music mixes in this case. Prior to iOS 6 when I rech same situation one audio pauses automatically and the mixing up does not occur. So kindly suggest some solutions.
Try to set
Required background modes -> App plays audio
in Info.plist.
As you can suggest, I have a trouble =)
In the game I use CocosDenshion for playing both background music and sound effects. As far as I know and can see in the code, it uses AVAudioPlayer to play background music. The trouble is appeared when I try to launch my app with music from iPod library.
I set audio category to AmbientSound (tried setting both with AVAudioSession and C interface). If I then try to play game background track directly using AVAudioPlayer, it plays with iPod music in background without problems. But if try to play background music with SimpleAudioEngine, only iPod music is playing.
Can someone give an advice how to make CocosDenshion work in this case?