Linuxmint
Linuxmint

Reputation: 4746

can I play a sound while the iPhone is sleeping?

How can I play a custom, looping sound (from my app, while my app is open) while the iPhone is sleeping?

I'm using this code to play a audio file:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sample_name" ofType:@"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;

// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];

if(audioError != nil) {
    NSLog(@"An audio error occurred: \"%@\"", audioError);
}
else {
    [testAudioPlayer setNumberOfLoops: -1];
    [testAudioPlayer play];
}

But it doesn't play if the iPhone is sleeping.

Pandora has this ability, along with other similar apps.

Upvotes: 1

Views: 912

Answers (2)

NWCoder
NWCoder

Reputation: 5266

If you are playing a sound when the phone goes to sleep, you will continue to run in the background as Pandora and others do.

If you are not playing a sound when the user puts the device to sleep, you will be suspended.

There's a reference in this answer to the apple documentation for this. Play sound with screen turned off / don't let iPhone go to sleep

Upvotes: 2

hotpaw2
hotpaw2

Reputation: 70743

Try registering for a local notification.

Upvotes: 0

Related Questions