V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
parkman
V2EX  ›  iDev

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

  •  
  •   parkman · 2014-04-03 18:36:00 +08:00 · 4787 次点击
    这是一个创建于 3682 天前的主题,其中的信息可能已经有所发展或是发生改变。
    类似使用百度时,即使iPhone静音,依然可以播放语音。

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

    有没有人懂这一块, 谢谢
    7 条回复    1970-01-01 08:00:00 +08:00
    loading
        1
    loading  
       2014-04-03 18:41:38 +08:00 via iPhone
    选择“静音”,然后开av,结果…啊,呃…

    去你妹的开发者
    parkman
        2
    parkman  
    OP
       2014-04-03 18:42:34 +08:00
    @loading 性情中人
    vicky517
        3
    vicky517  
       2014-04-03 22:02:07 +08:00
    @parkman 这种功能是哪个产品狗提出来的?
    tonyup
        4
    tonyup  
       2014-04-03 22:35:26 +08:00   ❤️ 1
    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
        5
    parkman  
    OP
       2014-04-04 09:18:24 +08:00
    @vicky517
    @loading

    一款汽车驾驶安全软件,有些警告语音必须要能播放,比如汽车追尾,车道偏离。
    parkman
        6
    parkman  
    OP
       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
        7
    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键值。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1799 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:46 · PVG 08:46 · LAX 17:46 · JFK 20:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.