IOS: Run application audio and phone music audio together in background - iphone

I need my application audio to still run even though it goes into the background with device music audio. I try the codes suggestion in this page but my application still doesn't work.
Set Required background mode to App plays audio
Set Application does not run in background to YES
AppDelegate.m
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

Here you can find a related question about it.
How to handle background audio playing while iOS device is locked or on another application?
I would suggest you to take a look a this tutorial:
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_background-audio/
It explains all the different steps to follow.

Related

Play sound in background using AVAudioPlayer without stopping ipod music

In my app at particular time i am alerting user with beep sound which i am playing using AVAudioPlayer.
Now i want this sound to be played even when app is in background. So i can achieve this by setting AVAudioSession category as AVAudioSessionCategoryPlayback and i also want that this sound should not stop the ipod music and i can do it with it setting AVAudioPlayer category AVAudioSessionCategoryAmbient.
But i want them together means sound should also play in background and it should also not stop the ipod music.
So how can i achieve this?
If it is not possible to play sound with AVAudioSessionCategoryAmbient category in background then any other workaround this?
Apologies if i am doing any mistake.
Thanks.
you need to add one row in your project.plist file Required background modes like bellow image and set it.
and put bellow code in to your project appdelegate didFinishLaunchingWithOptions
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
NSError *error;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if (!success) {
//Handle error
NSLog(#"%#", [error localizedDescription]);
} else {
// Yay! It worked!
}
its working for me hope its useful for you my frnd al the best.
UPDATE
You can't run AVAudioPlayer and the iPod player or MPMusicPlayer or MPMoviePlayer at the same time, without doing a bit more work. If you want easy, then use Audio Toolbox's System Sounds.
please refer this bellow link:-
iPhone AVAudioPlayer stopping background music
Please check bellow Link there are demo of Playing audio file in background. please check it
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_background-audio/

avaudiorecorder interruption in background

In my application I am using AVAudioRecorder for recording. Its works fine. but while I am recording in background if a device audio is played my recording get interrupted. I have the delegate function in my code. But its not getting called.
An app that plays audio continuously (even while the app is running in the background) can register as a background audio app by including the UIBackgroundModes key (with the value audio) in its Info.plist file. Apps that include this key must play audible content to the user while in the background.
Check below links
Link 1
You need to make couple of changes in plist file.
1) Set Required background mode to App plays audio
2) set Application does not run in background to YES.
3) Add following code to AppDelegate
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

How to play audio when Application enters in to the background using AVAudioPlayer

I am new to iphone.I am working on audio player which is used AVAudioPlayer.By using this framework i develop one application it plays the song when the application in active state but whenever i click home button in iphone it will immediately stops the audio in background but the application is running in background how to listen the audio when application is in background.
If any body know this please help me..
In the info.plist add key name Required background modes, Item 0 - App plays audio and when you are start to play audio add below code.
[[AVAudioSession sharedInstance] setDelegate: self];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];

iPhone OS 4 Multitasking - Playing Audio In background

I am trying to use iPhone OS 4.0's multitasking capability. I tried to play audio in the background with no luck. I added UIBackgroundModes property in info.plist and mentioned requires audio to play in background. Also I added the code for playing the audio.
`
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"someday" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];
`. The audio starts playing once i click on button in the app. But when I shut the app it stops. How can I make it play in the background?
Thanks,
Tony
It sounds like you didn't set up your Audio Session correctly. From http://developer.apple.com/iphone/library/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html :
For example, when using the default audio session, audio in your application stops when the Auto-Lock period times out and the screen locks. If you want to ensure that playback continues with the screen locked, include the following lines in your application’s initialization code:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
The AVAudioSessionCategoryPlayback category ensures that playback continues when the screen locks.
Activating the audio session puts the specified category into effect.
HI,
I think this video helps u in solving ur problem...
IN WWDC videos they have clearly explained how u can enable the back ground audio...
http://developer.apple.com/videos/wwdc/2010/
to view or download these videos u need to have a apple account...
and in that see Session 109-Adopting multitasking on iPhone OS, Part2...
Hope this will help u..
~Raviraja

AVAudioPlayer stops playing the audio when iPhone goes to sleep

I need to play a background sound all the time as long as my application is running. I tried to use AVAudioPlayer for this purpose. However, it stops playing the sound as soon as the iPhone goes to sleep mode.
Could you let me know how can I fix this problem?
Thanks
You may need to set an audio session category. From the "iPhone Application Programming Guide"
A category is a key that identifies a
set of audio behaviors for your
application. By setting a category,
you indicate your audio intentions to
iPhone OS, such as whether your audio
should continue when the screen locks.
The details about each category are listed at "Audio Session Categories"
In your particular example you may try the following. It should work right out of the box.
// Registers this class as the delegate of the audio session.
[[AVAudioSession sharedInstance] setDelegate: self];
// Allow the app sound to continue to play when the screen is locked.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
well this solution may also be helpfull
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
referenced from
http://www.mindyourcode.com/ios/iphone/how-to-play-audio-in-iphone-sleep-mode/