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];
}
Related
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.
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];
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];
I'm using AVAudioPlayer to play music (background supported). My question is if I want to call somebody while I'm listening to the music, how to resume it after the call? I've implement the AVAudioPlayer's delegate method:
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)thePlayer {
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[self.player play];
}
but this will not allow the music to continue.
I've also tried to use the AVAudioSessionDelegate method (just a try):
- (void)viewDidLoad {
[[AVAudioSession sharedInstance] setDelegate:self];
}
- (void)endInterruption
{
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[self.player play];
}
but again this won't cause the music to resume. Any ideas about how to solve this problem?
strange from your code is, you're telling that you do use AVAudioPlayer, why do you then implement AVAudioSessionDelegate?
And you have bad implementation of delegate methods. See docs
from AVAudioPlayerDelegate:
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
So try to implement them like this, and it should be working properly
On iOS, an app can not start playing audio when already in the background. If your app's audio is stopped because a foreground app has taken control of the audio session, there is (currently) no way to take it back.
You can resume it. After finishing your audio session you have to notify other app that my app deactivate the audio session and you can make use of it. Use the following code.
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
I ran audio in the background in my iPhone app using AVAudioPlayer and AVAudioSession...
Now my problem is that I already have a written code that takes a number of songs as input from the user and plays them one after the other using AVAudioPlayer...
The problem is that when the app goes to the background while one of the files is being played, the audio continues but when it's done, the sound disappears... In foreground when a file is done, I just do some code to play the next one but in the background this is not possible...
I thought about requesting some time to get this done and run my code but I can't do this as the audio files always change and they might be long... So please tell me what should I do? Is there any built-in playlist player that run as a background audio? or is there any other solution? I really need to get this into work...
thanks a lot
I ran into the same issue and haven't completely resolved it yet. The audioDidFinishPlaying delegate method still fires, and you can use UIAplication's beginBackgroundTask to create a new instance of AVaudioPlayer, make sure AVAudioSession is still active, etc. But when I try to play the new AVAudioPlayer instance, it pauses immediately. My suspicion/fear is that there is no way to start the audio FROM the background, only keep it playing when the application state transition happens. I'd love to be wrong, though.
this is easy ..just change the cateogry if your using an audio session that is ...
http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html
the link has the categories ...so use one of these and you be able to start the sound in the background:
AVAudioSessionCategoryPlayback
kAudioSessionCategory_MediaPlayback
the ambient one might not work.
//ViewController.h ,write below code
#interface ViewController : UIViewController<AVAudioRecorderDelegate,AVAudioPlayerDelegate>
//assign property to player
#property(nonatomic,retain) AVAudioPlayer *player;
//then write in ViewController.m file in ViewDidLoad Method below code
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
NSError *soundError;
NSString *path=[[NSBundle mainBundle]pathForResource:#"soundFileName" ofType:#"mp3"]; //.mp3 file for player
NSURL *file=[[NSURL alloc]initFileURLWithPath:path]; //path
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:file error:&soundError]; //player Object
if(_player == nil)
{
NSLog(#"player is empty because of %#",soundError);
}
else
{
[_player play];
_player.volume=1.0;
[_player setDelegate:self];
}
// for stop player you can use
// [_player stop]; //uncomment this line when you wants to stop it.