[请教] 当 iPhone 静音时,如何在 app 中还能播放语音。

2014-04-03 18:36:00 +08:00
 parkman
类似使用百度时,即使iPhone静音,依然可以播放语音。

试了一下下面的解决方案,没弄成功:
http://stackoverflow.com/questions/10322040/how-to-play-a-sound-using-avaudioplayer-when-in-silent-mode-in-iphone?rq=1

有没有人懂这一块, 谢谢
4818 次点击
所在节点    iDev
7 条回复
loading
2014-04-03 18:41:38 +08:00
选择“静音”,然后开av,结果…啊,呃…

去你妹的开发者
parkman
2014-04-03 18:42:34 +08:00
@loading 性情中人
vicky517
2014-04-03 22:02:07 +08:00
@parkman 这种功能是哪个产品狗提出来的?
tonyup
2014-04-03 22:35:26 +08:00
Silent mode是允许播放音频的(例如播放音乐的需求)。

注意三点:
1. 在Info.plist中加入UIBackgroundModes audio键值
2. 设置Audio session为AVAudioSessionCategoryPlayback(还有其他几个模式也可支持,具体见官方文档)
3. 音量不能为0(Mute)

官方文档: https://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html#//apple_ref/doc/uid/TP40007875-CH10-SW1
parkman
2014-04-04 09:18:24 +08:00
@vicky517
@loading

一款汽车驾驶安全软件,有些警告语音必须要能播放,比如汽车追尾,车道偏离。
parkman
2014-04-04 15:05:29 +08:00
@tonyup

1. 在Info.plist中加入UIBackgroundModes 加入了值 App plays audio or streams audio/video using AirPlay


2. 3.
NSError *audioSessionError = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];

if ([audioSession setCategory:AVAudioSessionCategoryPlayback
error:&audioSessionError]){
NSLog(@"Successfully set the audio session.");
} else {
NSLog(@"Could not set the audio session");
}

NSString *fileName = [NSString stringWithFormat:@"warning_yellow"];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
self.audioPlayerRed = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
self.audioPlayerRed.delegate = self;
self.audioPlayerRed.meteringEnabled = YES;
self.audioPlayerRed.volume = 1.0;
[self.audioPlayerRed play];




设置了音量为什么还不能正常播放
tonyup
2014-04-04 16:37:36 +08:00
如果是Show Raw key/value模式下,在UIBackgroundModes中添加audio键值。否则在Required background modes中添加App plays audio or streams audio/video using AirPlay键值。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/107152

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX