iPhone - Resuming background music after playing movie - iphone

I'm playing iTunes music in the background.
I started my app which plays "Video" using the following code:
NSString* videoPath = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"m4v"];
if (player) {
[player release];
player = nil;
}
player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:videoPath]];
[self presentModalViewController:player animated:YES];
I'm setting the audio session as follows:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error:nil];
When my video starts playing, iPod music playing in the background will be stopped.
How can I resume the background music when my video completes?

This kind of option to continue must from ipod is not possible.

Use notifications
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
- (void) moviePlayBackDidFinish {
// resume your AVAudioSession
}

Related

Is it possible to autoplay audio and play/pause with button?

I need to play audio automatically after open the view and have one button to control play/pause event. I heard that it has a prepareToPlay to control the audio by button but it cannot play audio automatically.
Here is my code:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"track8"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer prepareToPlay];
and in the button:
-(IBAction)playPauseButtonPress:(id)sender{
if (self.isPlaying) {
[self.audioPlayer pause];
self.isPlaying = NO;
}else {
[self.audioPlayer play];
self.isPlaying = YES;
}
}
Is it possible to autoplay audio and play/pause with button? How?
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"track8"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer prepareToPlay];
There is method play is available for playing it. You just need to add following line:
[audioPlayer play];
Please check the AVAudioPlayer documentation
play >
Calling this method implicitly calls the prepareToPlay method if the audio player is not already prepared to play.
prepareToPlay > Calling this method preloads buffers and acquires the audio hardware needed for playback, which minimizes the lag between calling the play method and the start of sound output.Calling the stop method, or allowing a sound to finish playing, undoes this setup.
There is nothing like you can't pause the audio if you use play instead of prepareToPlay.
Hope this helps

iPhone App : Starting to play music very slow

Am working on an iPad/iPhone App & am playing a background music with this code snippet which I call in the Delegate's didFinishLaunchingWithOptions method:
-(void) playMusic {
NSLog(#"play music!");
NSString *sound_file;
if ((sound_file = [[NSBundle mainBundle] pathForResource:#"musicfile" ofType:#"mp3"])){
NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.audioPlayer.delegate = self;
[self.audioPlayer setNumberOfLoops:-1]; // Infinite loop
[self.audioPlayer prepareToPlay];
[self.audioPlayer setVolume:0.2];
[self.audioPlayer play];
} else {
NSLog(#"WARN : music file not found!");
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
But I see that this method alone is taking 0.43 seconds on an average, which is a lot! Am I doing it the right way? Or is there a faster way to start the music play ? Please help.
My environment : iOS5, XCode 4.3, iPad 2.0 is what I tested this with now

How to play an audio when home button clicked in iphone sdk

Now am developing an application in which the application will play the audio automatically when the home button is tapped (when entering the background mode), below is my code i've used. it works perfectly in simulator but not working in any of the devices (iPhone, iPad, iPod). please guide me to go further...
- (void)applicationDidEnterBackground:(UIApplication *)application {
AVAudioSession * session = [AVAudioSession sharedInstance];
[session setDelegate: self];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#.wav", [[NSBundle mainBundle] resourcePath], soundName]];
AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer setVolume:1.0];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSAssert(backgroundTask == UIBackgroundTaskInvalid, nil);
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}
I don't think you can do this, when the app is going into the background audio is muted thus the sound you play will nog be heard.
And this will be very annoying and should just not be done, also the simulator reacts differently than then a real iOS device.
An app can only play audio that has been started before entering background mode. Note that an app can play silent audio, but Apple has rejected apps that play a lot of silence.

AVAudioPlayer cannot play music with a NSTimer in background

I want to play music with an AVAudioPlayer using an NSTimer, but [player prepareToPlay] returns NO & doesn't play in the background.
Can somebody give me some idea?
Here's my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(playMusic:) userInfo:nil repeats:NO];
[self.window makeKeyAndVisible];
return YES;
}
- (void)playMusic:(NSTimer *)timer{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
NSString *fileName = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"Apologize.mp3"];
NSURL *fileUrl = [NSURL fileURLWithPath:fileName];
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
if ([player prepareToPlay]) {
[player play];
NSLog(#"start!");
}else{
NSLog(#"error msg :%#",error);
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
if ([[UIApplication sharedApplication] respondsToSelector:#selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
UIBackgroundTaskIdentifier backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// /* just fail if this happens. */
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
}];
}
You need to do this init in the viewDidLoad :
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"test"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
This is for the audio initialization. Here, with a MP3 named test, imported in the project.
Note that the beginReceivingRemoteControlEvents part is very important. Without it, you cannot start playing from background, just keep on listening to music already playing from foreground.
Now you need to listen to the event didEnterInBackGround (you could go with the applicationDidEnterBackground of your app delegate, but this way you can put the code in the class it belongs to):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didEnterBG:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
And now in that didEnterBG method :
self.backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(backgroundWork) userInfo:nil repeats:YES];
[self.backgroundTimer fire];
Here i make this sound repeat every 10s for personal reasons, but do whatever you need.
Finally, the backgroundWork method :
- (void)backgroundWork{
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
}
Now your audio file is playing in background :-)
You should also note that your app need to have specific rights to keep doing jobs in background. You need to check the box under 'YourTarget' -> Capabilities -> Background Modes : Audio and AirPlay. Otherwise, the OS will terminate you app in a few minutes in real conditions (with no debugger or instrument).
before going to code. you need to tell the OS that you want permission to play music in background.
you can do that by setting a Key in your app's info.plist
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
doing so, you will be able to play audio when app moves to background.
I have the same issue, but I found the solution from this post: http://developer.apple.com/library/ios/#qa/qa1668/_index.html
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) { /* handle the error condition */ }
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) { /* handle the error condition */ }

Application getting crashed on playing some sound file on iPhone application?

I am trying to play some music files on iPhone but after playing some files application getting crashed. It's random in nature and I am not getting any particular scenario can any one help me out on this?
My Code :
[player stop];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#",crr_PlayFile] ofType:#""]] error:nil];
[player setDelegate:self];
[player prepareToPlay];
[player play];
Is there any problem in my code.
Try to
[player release];
player = nil;
after your [player stop] call.