App is not working in the background mode - iphone

i'm working on an audio based app. audio is playing in the background but when the track changes it stops. and when again i open the app it resumes and plays the next track.
i'm using audio streamer in my app
all is working fine when app is in the foreground.
Please help me with this
Thanks in advance.

You have to add the Required background modes modes to the info.plist and add the mode App plays audio.

try to add these line in your app delegate -
//enable remote control event for app
if([[UIApplication sharedApplication] respondsToSelector:#selector(beginReceivingRemoteControlEvents)])
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// allows you to play in the background when app is suspended in iOS4
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];

Related

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

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.

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];

MPMoviePlayerController multitasking problem

I develop an application on iphone that uses MPMoviePlayerController to play audio file.
When Application goes to background, if mpmovieplayer is playing, iphone continues to play the current music, but when it ends, the next track doesn't start.
I try to start the next audio file in the MPMoviePlayerPlaybackDidFinishNotification and when I follow the code using the debugger I can see that the method is invoked and the code executed, bat the next audio file still doesn't start.
Is this possible on iOS 4.1 or this is a limitation?
Best regards
Samantha
You should read the Technical Q&A QA1668:
How to play audio in the background with MPMoviePlayerController.
Summary of the steps needed:
declare that your application supports background execution for audio
assign an appropriate category to your audio session (default category is not ok)
I got it working by putting following two lines in Delegate file:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
may be you forget to add this in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

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/