how to handle the interruption in iphone - iphone

I have problem with AVAudioPlayer in iphone when ever the my ipod is lock(sleep) the song is stop.What I have do in coding for making music is continueslu start even the ipod is lock(sleep) .
please help me
I am totally new in this .

You can use different categories to do this:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
...
// allows you to play when the screen is locked and also when the ringer is set to silent
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
...
}
If you want to continue to play the sound on iOS4 when your app gets suspended into the background (for instance, the user hits the home button), then you also need to add this to your info.plist file (this shows it in plain text mode):
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
By the way, if you ever really needed to handle an interruption like your question title said (like a phone call, etc), there is a delegate method for that in the AVAudioPlayer class:
- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player { }
and
- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player { }

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/

iphone app allow background music to continue to play

When I launch my iPhone game as soon as a sound plays the background music or podcast that is playing stops. I noticed other games allow background audio to continue to play.
How is this possible? Do I need to override a method in my App Delegate?
Place this line in your application:didFinishLaunchingWithOptions: method of your AppDelegate or in general before using the audio player.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
According to the documentation, the AVAudioSessionCategoryAmbient category is
for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.
This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays over iPod audio. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).
If you want also to ensure that no error occurred you have to check the return value
NSError *error;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if (!success) {
//Handle error
NSLog(#"%#", [error localizedDescription]);
} else {
// Yay! It worked!
}
As a final remark, don't forget to link the AVFoundation framework to your project and import it.
#import <AVFoundation/AVFoundation.h>

How to resume background music after out-going phone call (iOS)?

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

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

How to play multiple audio files in the background in iOS 4?

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.