关闭屏幕播放声音

关闭屏幕播放声音

本文介绍了关闭屏幕播放声音/不要让iPhone进入睡眠状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个应用程序,即使屏幕关闭,我也希望它保持正常运行。
以前当我想要这样做时,我使用了这个黑客/技巧 - 我在后台循环播放静音/空声(AudioServicesPlaySystemSound),所以如果用户按下开/关按钮,应用程序仍可在后台运行 - 所以它永远不允许iPhone进入睡眠模式 - 它只是关闭屏幕,可能是wifi或蓝牙(就我记得的iPod Touch加速度计)。它奏效了。我想在我的新应用程序中使用相同的技巧,但是当我现在测试它时,似乎它不再起作用了。背景中的声音播放(当我用一些声音替换空音频文件时我可以听到它播放)即使屏幕关闭但它应播放的声音(使用AVAudioPlayer)也不播放(即使我转动屏幕)再次)。
我不知道它何时停止工作(肯定是在3.x操作系统上工作)。难道我做错了什么? Apple是否更改/修复了黑客,即使屏幕关闭,您的应用也可以正常工作?还有另一种方法可以让设备进入睡眠状态(并减少耗尽电量)但继续工作吗?

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]; }
 }
}

代码有点复杂 - 可能只是前几行 - 它是这样的,所以你只能播放X秒的声音(如果声音比X短,它将播放几个循环,直到总时间> = X)。当然,当屏幕保持开启时一切正常。

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.

此外 - 如果你发现你的项目中有用的代码(比如playSound:withLoops:stopAfter :) - 随意使用它(但如果你给我发消息,那会很酷,所以我知道我帮助了:))。

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 :)).

推荐答案

所以答案是设置会话类别。某些类别只是在设备被锁定时关闭声音。对我来说,最好的选择是 AVAudioSessionCategoryPlayback

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];

这篇关于关闭屏幕播放声音/不要让iPhone进入睡眠状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 14:00