开发者

Play sound with screen turned off / don't let iPhone go to sleep

开发者 https://www.devze.com 2023-01-26 23:06 出处:网络
So I have an application and I want to keep it working even if the screen is turned off. Previously when I wanted to do that I used this hack/trick - I play a silent/empty sound in a loop in the backg

So I have an application and I want to keep it working even if the screen is turned off. Previously when I wanted to do that I used this hack/trick - I play a silent/empty sound in a loop in the background (AudioServicesPlaySystemSound) so if user presses the on/off button the application still works in the background - so it never allow iPhone to go to sleep mode - it just turned off the screen and maybe things like wifi or bluetooth (and on the iPod Touch accelerometers as far as I remember). And it worked. I wanted to use the same trick in my new application but when I was testing it now it seems it doesn't work anymore. The sound in the background plays (when I replace "empty" audio file with some sound I can hear it play) even with screen turned off but the sound it should play (using AVAudioPlayer) doesn't play (even when I turn the screen on again). I don't know at which point it stopped working (it worked on 3.x OS for sure). Am I doing something wrong? Did Apple changed/fixed the "hack" that allowed your app to 开发者_开发百科work even with screen turned off? Is there another way to allow the device to go to sleep (and drain the battery less) but continue to work?

This is the code I use to play background/silent sound:

-(void) playSilentSound
{ 
 CFBundleRef mainBundle = CFBundleGetMainBundle ();
 CFURLRef silentUrl = CFBundleCopyResourceURL (mainBundle, CFSTR ("silence"), CFSTR ("aiff"), NULL);
 AudioServicesCreateSystemSoundID (silentUrl, &silentSound);
 silentTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:@selector(playSilence) userInfo: nil repeats: YES];
}

-(void) playSilence
{
 AudioServicesPlaySystemSound (silentSound);
}

And this is how I play the sound that should play even if the screen is turned off:

-(BOOL) playSound: (NSString *) path withLoops: (BOOL) loops stopAfter: (int) seconds
{
 NSError *error;
 player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: &error];
 player.delegate = self;
 player.numberOfLoops = 0;
 player.volume = volume;
 secondsPlayed = 0;
 loop = loops;
 BOOL played = [player play];

 if(played && seconds > 0)
 {
  timer = [[NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector: @selector(stopPlaying:) userInfo: [NSNumber numberWithInt: seconds] repeats: YES] retain];
  secondsLimit = seconds;
 } else {
  secondsLimit = -1;
 }
}

-(void) stopPlaying:(NSTimer*)theTimer
{
 if(secondsLimit > 0 && (secondsPlayed + [player currentTime]) >= secondsLimit)
 {
  [player stop];
  [timer release];
  timer = nil;
 }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)sender successfully:(BOOL)flag
{
 if(sender == player)
 {
  if(flag) { secondsPlayed += sender.duration; }
  if(loop) { [player play]; }
 }
}

The code is a bit complicated maybe - it could be just the first few lines - it's made that way so you can play only X seconds of sound (if sound is shorter than X it will play few loops until the total time is >= X). And of course everything is working fine when the screen is left on.

Also - if you find the code useful in your projects (like playSound:withLoops:stopAfter:) - feel free to use it (but it would be cool if you send me a message so I would know that I helped :)).


So the answer is to set session category. Some categories just turn off sound when the device is being locked. For me the best option was AVAudioSessionCategoryPlayback

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];


I don't know about Apple fixing stuff, but in iOS 4 there are now official ways to continue doing things in the background which you should take advantage of.

0

精彩评论

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

关注公众号