setIdleTimerDisabled and MPMusicPlayer Issue - iphone

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!

Related

audioPlayerEndInterruption does not work if CALLER cancels

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.

iOS idleTimerDisabled behavior

My iPhone application has two states: UI and Game.
Game is played using device tilting only so I switched auto-sleep off on game start:
[[UIApplication sharedApplication].idleTimerDisabled = TRUE;
But as soon as I return to UI, I want auto-sleep to be active again. So on game finish I restore it:
[[UIApplication sharedApplication].idleTimerDisabled = FALSE;
After a long game playing, it resulted in immediate darkening of the first UI screen that I go after the game.
So it seems that when idle timer was disabled it was still calculating time. And it had fired immediately after "enabling". How can I fix this problem?
I don't know if the idleTimer can be reset programmatically, but an option is to require the user to touch the screen before it goes back.
Another option is set your own timeout once you are back and wait for it to complete before you set idleTimerDisabled = NO. Remember to clear this timeout if you start the game again.
This answer may be usefull.
I also had problems when using Music/Audio players, which seemed to reactivate the timer.
ps: in ObjectiveC you should use YES/NO instead of FALSE/TRUE

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/

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

idleTimerDisabled not working since iPhone 3.0

I have used:
[UIApplication sharedApplication].idleTimerDisabled = YES;
in a number of Apps developed and running under iPhone OS 2.x and never had any problems with it. They were clock apps so needed to run constantly and ignore the iPhone's idle Timer setting.
However, trying to achieve the same with a new App running OS 3.0 (and which needs to be deployed under 3.0 as it uses some 3.0 APIs) I've found the idle Timer to be either ignored or inconsistent.
My App plays music from the iPod library and when the music is playing it auto-locks regardless of the above setting. But once you unlock it, it then doesn't auto-lock again unless you play music again, in which case it locks again after the iPhone auto-lock time setting.
I'm amazed no-one else has come across this as I imagine it would affect a large number of Apps.
Just to clarify:
1. The above code is in ApplicationDidFinishLaunching
2. I know that the phone won't auto-lock when testing from xCode regardless of settings
If anyone has any thoughts I'd be very grateful...
Our app uses the MPMediaPLayer. We also had the idleTimerDisabled=YES code in the ApplicationFinishedLaunching, which works EXCEPT if untethered, and there is already a current nowPlayingItem which is left playing (or unpaused, if paused at app startup). Obviously this is all with the Settings -> General -> Autolock set to some timed value.
By adding idleTimerDisabled=NO, immedately followed by idleTimerDisabled=YES in one of the other bits of code AFTER we had figured out what bit of music we would get playing seemed to solve the problem. Just setting it to YES was insufficient.. and subsequent queries had always indicated the correct value (YES).. so it appears the Apple code ignores the setting of the value IF there is a current piece of music and that is not changed by your code.. but does notice a change of value.
This is all under iOS 3.0.
Even in 2015, using iOS 8.2, this bug is still alive and kicking.
Here's my solution, using XCode 6.2.
iPhone - phone goes to sleep even if idleTimerDisabled is YES
Basically, even now, in 2015, the only way to safely make sure that the device doesn't go to sleep is to repeatedly call a piece of code to keep the device awake.
-(void)callEveryTwentySeconds
{
// DON'T let the device go to sleep during our sync
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
Sounds like a bug, file with Radar - I am not too surprised this has not been seen much as there are probably not a lot of apps that try to lock the screen open and play music.
Having same issue. It does work when the device is plugged in. You can press lock button on top, and my NSTimer fires later and causes a vibrate. However if the device is not plugged in pressing the lock button puts the device to sleep. Any solution would be greatly appreciated.
iCodeblog posted about the idletimer, I said it didn't work, and the person who develops 'cute clock' was nice enough to reply. You have to do a hack, play a 1 second or longer silent sound every 10 or so seconds with NSTimer. This keeps the device awake even if the user hits the lock button.
I develop Seconds - Interval Timer for iPhone and iPod touch and I've had no end of trouble with this. The idea of my app is that people create timers based on a number of intervals where each interval can have it's own playlist or track played.
In iOS3 I had the problem that I couldn't disable the idle timer by just setting idleTimerDisabled = YES. In the end I came up with the same solution as Neil whereby I would periodically set it to NO, then immediately to YES again. This seemed to work.
I'm now updating the app to iOS4 (I know, iOS5 is just around the corner...) and now I have the opposite problem. If the MPMediaPlayer changes track before the idle timer reaches it's limit it gets reset. I've just tested this by creating an interval in my app that was 55 seconds, my auto-lock was set to a minute. At 50 seconds the screen dimmed as it prepared to lock, but at 55 seconds when the music changed it went back to full brightness and then didn't lock as it should.
Overall, the implementation of this seems flakey at best.