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];
Related
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/
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 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 { }
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.
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/