AVFoundation playing a sound is stopped when playing background music - ios

I play a sound with AVFoundation. No problem with that. But when I play music the sound gets stopped. There is some way to play the two sounds simultaneously?
I tried with:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
before playing the sound without success.

Related

iOS Increase volume of each sound played

I am using AVAudioSession and AVAudioPlayer calling the steps below in my code. However, the sound is not playing on my specified number.
Here is the code:
[playSoundURL1 play];
[playSoundURL1 setVolume:(0.5)];

Play music in background and only stop in certain instances

I have an app that shows videos in a timeline. The videos autoplay and by default they should be silent. If the user is playing music in the background, music should not stop at this time. It is only when tapping the video that it should actually pause the background music and let the video play. And once the video is done playing, the user's previous audio should continue where it left off.
I've tried using
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
but this lets the music mix with the audio even if the video is in full screen mode. Of course, I could ensure that when the play button is tapped, the music pauses, but that might not be the cleanest solution.
Any better ideas?

MPMusicPlayerController Play music while app is in background

Hope this question doesn't get down voted - I haven't seen anything in the documentation.
I want to play music from the ipod library while the app is in the back ground.
I am creating the music player using the default mechanism.
-(void)viewWillAppear:(BOOL)animated
{
MPMusicPlayerController *myPlayer =
[MPMusicPlayerController applicationMusicPlayer];
// assign a playback queue containing all media items on the device
[myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
// start playing from the beginning of the queue
[myPlayer play];
}
As soon as the app enters the background the music stops playing. I would like it to continue playing though.
I do not want to use the iPodMusicPlayer option
How can I do this?
You should set background audio mode in the project settings (Project settings > Capabilities > Background modes > Audio and AirPlay) like it's shown on the screenshot below:
Also you should set Audio Session Category in the appDelegate (in the didFinishLaunching method). Example is shown below:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
I have same issue. I searched even after added flag in plist. It will not work because :
The applicationMusicPlayer does not support background music. Use
MPMusicPlayerController's systemMusicPlayer instead. It shares the
state with the built-in System player and music will continue to play
when your app enters the background.
Other Solution is :
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.
Note : iPodMusicPlayer is deprecated. so Instead of this use systemMusicPlayer.

How can I play a video without causing music on iTunes/other audio to pause

I'm using AVPlayer to play a video (open to alternative players). My videos don't have sound. When the video starts, if the IOS device is playing music/audio of some sort, the music/audio pauses even though the video has no sound.
Is there any way to continue playing music while playing a silent movie?
Try preparing the audio session before starting playback:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setActive:NO error:nil];
//... create & configure AVPlayer and start playback

Playing a silent movie with AVFoundation and keeping iPod music playing

I have an app that plays silent Movies using AVFoundation. Is there a way to allow the music to play and your iPod (if you're playing music) to continue playing? If I play the movie will my iPod is on, the iPod pauses so the movie can play, and vice versa - if I try to replay the music while the movie is playing, the movie pauses. Basically, I want them to play continue playing seamlessly....
Let me know if you need applicable code to answer.
In my experience, for standard AVAudioPlayer based sound in an app, managing the AVAudioSession is required to allow background music:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
But I can't tell you if that's the same as attempting to play encoded/compressed Video and encoded/compressed audio at the same time.

Resources