Reputation: 4905
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];
//[player setVolume: 0.50];
player.numberOfLoops = -1;
[player setDelegate: self];
[player play];
} Thank you.
Upvotes: 0
Views: 1577
Reputation: 9173
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.
Upvotes: 2