views:

192

answers:

2

Hi

I am implementing some audiobooks for iPhone. I used AVFoundation. Something like this:

NSString *path = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mp3"];
     player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

I have a problem. When the screen goes dark (single audio files can be very long) the audio stops playing.

I solved this problem with this string of code

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // something else here...

    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

}

This does not allow the iPhone to "sleep". However you can guess how this is foolish: your battery level goes down in minutes and this is not possible by audiobooks lasting more than 20 hours, for example...

So, do you know a way to prevent that when the screen sleeps the AVAudioPlayer does not stop playing?

Thanks... Fabio

A: 

Could I suggest that once you start playing the audio file that you tell the user to press the power button (not home button) which will lock the phone. It will not close you app but it will power off the screen with you application running in the background. Currently several apps do this.

John Ballinger
Thaks a lot John to take the time to answerI tested what you say, but it seems that the audio fades out after I press the power button and fade in after I press it again.AVFoundation seems to require the screen on... is this possible?Tried to use mediaplayer too... which I used to play video. But also in this case the audio fades out after I press that button... Really confused :)
+1  A: 

Set your audio session to kAudioSessionCategory_MediaPlayback to playback while the screen is locked.

Rhythmic Fistman
Hi thanks to your hint I found it:UInt32 category = kAudioSessionCategory_MediaPlayback; //1 AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (category), //2 AudioSessionSetActive (true); //3But is this referring to which framework, AVFoundation or MediaPlayer? Thanks :)
It refers to your application's audio requirements, and hence to any frameworks your application uses.
Rhythmic Fistman
p.s. "hint" or "answer"?
Rhythmic Fistman
it's already a miracle I wrote in English and you understood :) Be happy with hint ;)
so did the audio session work?
Rhythmic Fistman