开发者

iPhone:Background music stops when screen Off and doesn't continue when screen On?

I have a music file which runs in the background(non-stop) when my app is running. This is my requirement. But when my app is running, screen goes off as per auto lock time set, music is topped. And then again when switch on the screen, my application is coming there but music is not starting. Is it a known bug? (or) How to get screen ON notification so that i can play the music again. (or) how to resolve it?

I use this code to play the music always ... - (void) PlayLoopMusic {

NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
url = [NSURL fileURLWithPath:path];

player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player prepareToPlay];
//[playe开发者_开发知识库r setVolume: 0.50];
player.numberOfLoops = -1;
[player setDelegate: self];


[player play];

} Thank you.


In addition to Morion's answer:

You can disable automatic screen locking by the following call:

[UIApplication sharedApplication].idleTimerDisabled = YES;

You can also make sure that the playback goes on while the screen is locked manually. For this you have to create an AudioSession:

AudioSessionInitialize(NULL, NULL, audioInterruptionListener, NULL);
UInt32 category = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
    sizeof(category), &category);
AudioSessionSetActive(YES);

audioInterruptionListener should be a function that handles the cases when the audio playback is interrupted (for example during an incoming call). You can read more about this here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜