audioPlayerEndInterruption does not work if CALLER cancels - iphone

I am using audioPlayerEndInterruption to bring back game music after an 'interruption'.
It seems to work perfectly for all scenarios tested, except:
if a call is received but the caller hangs up (before being answered), the game comes back, but the audio track does not.
As I say, all other call interruption scenarios work, the track does come back. I wonder if this is some kind of iOS bug? Has anyone heard of this problem? Seems strange.
I'm inclined to forget it for the time being, but it is annoying!
My code is like this:
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags{
if(flags == AVAudioSessionInterruptionFlags_ShouldResume && player != nil){
[player play];
}
}
Would appreciate any advice. Thanks.

Better late than never.
I'm also experiencing this issue. It looks like a bug (at least in ios 5.1) because if all AVAudioPlayers are stopped, and openAL sounds are playing only, AVAudioSession delegate methods are called as expected (yet AVAudioPlayer's do not).
So if AVAudioPlayer is running I use a workaround:
just add to AppDelegate::applicationDidBecomeActive call to your audioPlayerEndInterruption handler with some checks if appropriate (if sound manager exist and so on). This works for me.

Related

Music not stopping when moving an app to the background

I am optimizing a game so that when there is an incoming call or if it is moved to the background for any other reason, it should stop the music, timers and pause correctly.
It works great other than the fact that for some reason the music doesn't stop playing even though I issued a command for it to stop. What's weirder is that when the game returns to the foreground there's 2 background musics playing instead of one.
Here is my some of my code, nothing too complicated or out of the ordinary:
...
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate);
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate);
...
private function handleActivate(event:Event):void
{
stage.frameRate = previousFrameRate;
//if the music was on when the game is moved to the background, then replay it when its moved back to the foreground
if(musicOn)
{
BGMusic.play();
}
}
private function handleDeactivate(event:Event):void
{
BGMusic.stop();
stage.frameRate = 0;
}
It is worth noting that if I don't replay the music when then game returns to the foreground (i.e. when I don't use BGMuusic.play() in handleActivate), there won't be any music as expected. It is only when I stop the music AND resume it later when moving to the foreground that the background music doesn't stop and I get two playing tracks.
Any ideas? As I said, everything else works fine, and the game correctly pauses. I am testing this on Flash builder Emulator since I don't have the necessarily certificates to test it directly on the iphone. So maybe it is a problem with the emulator itself rather than the code.
Thanks in advance
EDIT: Am writing this with flash and adobe air
Well that was stupid of me. Everything is working fine I just forgot to add another condition to the if statement. So instead of:
if(musicOn)
{
BGMusic.play();
}
It should be
if(musicOn && !BGMusic.isPlaying())
{
BGMusic.play();
}
Everything works fine now...I will change the variable name of musicOn to something clearer like musicButtonOn.
Anyways thanks for help. If you have any tips or comments feel free to tell me =D
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
NSLog(#"Playing stoped");
[player stop];
}

setIdleTimerDisabled and MPMusicPlayer Issue

Ok I want to disable the idletimer in my app. I put the code below in appdelegate in the applicationDidFinishLaunching method, and it works fine (the device remains unlocked)
[application setIdleTimerDisabled:YES];
Though, when I play a music through MPMusicPlayer methods, the idletimer seems to be enabled back again... and soon the device goes into dimmed mode and soon after, "locked" mode.
I tried to find in google for other methods, but ultimately there are NONE that are effective. I also tried putting the above code in another function and calling that function every seconds using NSTimer when the music is playing, but to no avail. It just won't work.
How can I solve this issue?
Ugh. I hate answering my own questions. But here you go, the answer is here:
How to stop MPMusicPlayerController from enabling screen locking
See the reply from henning. Works like a charm!

Sounds lost after interruption

I'm using the SoundEngine provided with Apple's crash landing example.
After an interruption such as an incoming phone call or an alarm I call to applicationWillResignActive: inside my delegate, in order to pause the game and save the state of it. After the interruption ends I return to my game but the sound is gone. Even if i reinitialize it with SoundEngine_Initialize() the game still wont reproduce any of the sounds, unless i restart my app.
How can I restore my game sounds after the interruption ?
Have you looked at the sound manager class used by 71squared ? You can likely just use their sound manager as is... but if you wanted to role your own, you can look at thier code as I know they have solved this in their code.
http://www.71squared.com/2010/01/latest-sound-manager-class/

how to know that MPMoviePlayerController is playing in Iphone OS 3.0

I need to know if at an specific moment the MPMoviePlayerController is playing.
In iphone 3.0 it is not firing the MPMoviePlayerContentPreloadDidFinishNotification.
Does anyone knows any solution?
Thanks in advance!
So thanks for the answer.
I fix this so I will post the answer.
The answer is that MPMoviePlayerContentPreloadDidFinishNotification is NOT FIRED if you invoke play() right after the initialization.
To have MPMoviePlayerContentPreloadDidFinishNotification which works ok you have to invoke the "play()" method of the MPMoviePlayerController when the MPMoviePlayerContentPreloadDidFinishNotification is fired ( I mean in the MPMoviePlayerContentPreloadDidFinishNotification method ). In this case it always works.
Tested on 3.0, 3.1 and 3.1.2
Same question (but no answer) here and several other reports can be found on other sites (e.g. here)
Not ideal, but it seems targeting 3.1 solves the problem.
A workaround maybe to set the MPMoviePlayerController property scalingMode to something other than the default MPMovieScalingModeAspectFit ( e.g. MPMovieScalingModeNone and make sure your video is correct size) before calling play and then hook the MPMoviePlayerScalingModeDidChangeNotification event instead. it seems to get called (more than once!) as soon as the movie starts playing. Of course it will also be called if the user changes the scaling mode manually, so code for this. It's dirty but may help you?

Can't stop movie playback immediately

When you launch a video and IMMEDIATELY press "Done" the MPMoviePlayerController will exit, however the video still plays in the background (you can hear the audio). It works fine if you allow the video to begin playing before you hit the "Done" button.
Does anybody know a workaround for this?
Try calling stop on your MPMoviePlayerController instance when the view controller that spawned the movie receives its viewDidAppear: (or viewWillAppear:) methods
While, so far, I've also found MPMoviePlayerController somewhat erratic, I imagine just releasing the object (after listening to a MPMoviePlayerPlaybackDidFinishNotification) would help.
Update: This seems to be a known 3.0 bug. I was unable to find a workaround
http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/15768-mpmovieplayer-video-via-url-glitch.html