iPhone 睡眠时可以播放声音吗?
如何在 iPhone 睡眠时播放自定义循环声音(在我的应用程序打开时从我的应用程序播放)?
我正在使用此代码来播放音频文件:
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];
}
但如果 iPhone 处于睡眠状态,则不会播放。
Pandora 以及其他类似的应用程序都具有此功能。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在手机进入睡眠状态时播放声音,您将继续像 Pandora 和其他人一样在后台运行。
如果用户将设备置于睡眠状态时您没有播放声音,您将被暂停。
这个答案中有一个对苹果文档的参考。
在屏幕关闭的情况下播放声音/ 不要让 iPhone 进入睡眠状态
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
尝试注册本地通知。
Try registering for a local notification.