【问题标题】:Playing an alert sound when the phone is on vibrate手机振动时播放提示音
【发布时间】:2014-10-22 19:44:45
【问题描述】:

我的应用程序在后台运行,轮询电子邮件服务器并在发生更改时发送本地通知。我可以从后台状态开始播放警报,但如果手机处于振动状态,则不会播放音频。有没有办法覆盖它,当振动打开时时钟应用程序的闹钟仍然响起?

在启动时我的应用委托中,我调用下面的警报类方法来设置在后台播放:

+ (void)initializeAudioSettings
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];

    NSError *setCategoryError = nil;
    BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    if (!success) {
        NSLog(@"Error setting up Audio session: %@", setCategoryError.description);
    }

    NSError *activationError = nil;
    success = [audioSession setActive:YES error:&activationError];
    if (!success) {
        NSLog(@"Error setting audio session active: %@", activationError.description);
    }

}

接下来我加载警报:

- (void)loadAlertSoundID
{
    NSLog(@"Loading alert sound");
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"alert" ofType:@"mp3"];
    NSURL *soundURL = [NSURL URLWithString:soundPath];

    CFURLRef url = (__bridge CFURLRef)soundURL;

    AudioServicesCreateSystemSoundID(url, &_alertSoundID);
}

然后当我收到新消息的通知时,我会调用我的警报方法:

- (void)playAlert
{
    NSLog(@"Playing alert");
    AudioServicesPlaySystemSound(_alertSoundID);

    _alertShouldBePlaying =  YES;
    _alertIsPlaying = YES;

    AudioServicesAddSystemSoundCompletion(_alertSoundID, nil, nil, playSoundFinished, (__bridge void*)self);

    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(vibrate:) userInfo:nil repeats:YES];
}

为了完成,我的完成块。:

void playSoundFinished (SystemSoundID sound, void *data)
{
    Alert *alert = [Alert sharedAlert];
    if (sound == alert.alertSoundID)
    {
        alert.alertIsPlaying = NO;

        // Repeat until alert is disabled
        if (alert.alertShouldBePlaying && !alert.alertIsPlaying)
        {
            [alert playAlert];
        }
    }

}

【问题讨论】:

  • 这是“把我赶出剧院”功能吗?
  • 我正在构建它作为在办公室内部使用的工具。有一个“下班”选项,它专门切换到振动,然后在第二天工作开始时自动重新上班。
  • 将手机置于振动状态的全部意义在于它不会发出声音。
  • 我知道,但就像我说的,这是供内部使用,而不是去应用商店。必须有办法做到这一点,因为时钟应用正是这样做的
  • Apple 应用程序可以做某事并不意味着第三方应用程序可以使用公共 API。时钟应用也可以更新其图标,但第 3 方应用不能这样做。

标签: ios objective-c core-audio audiotoolbox


【解决方案1】:

有一种方法可以做到这一点,但如果我是您的应用程序的用户,我不会感激这一点。

看看AVAudioSessionCategory。您可以将 AVAudioSession.category 设置为 AVAudioSessionCategoryPlayback 或 AVAudioSessionCategoryPlayAndRecord,这将绕过手机上的静音开关以及任何警报声音设置。不过,它可能仍受音量按钮控制。

编辑:您还需要使用AVAudioPlayer 或AudioUnit 来播放声音,而不是AudioServicesPlaySystemSound 以绕过静音模式。

【讨论】:

  • 我已经把它设置为播放了,我刚刚尝试了playAndRecord,但它进入时仍然无声
  • 对,我应该阅读你的整个帖子。您正在通过 AudioServicesPlaySystemSound 方法播放声音。如果您使用 AVAudioPlayer 或较低级别的 AudioUnit API,则可以绕过静音模式。我不相信 AudioServicesPlaySystemSound 尊重您的 AVAudioSessionCategory。
  • 好的,感谢您的洞察力。我要研究一下 AudioUnit,看看我能用它做什么
  • 为了您的目的,AVAudioPlayer 应该可以解决问题,AudioUnit 只是播放简单的声音有点过分
  • 是的,这是 iOS 的默认行为,可防止应用从后台对您大喊大叫。有办法解决它,但我认为它们会让你被 App Store 拒绝,这是有充分理由的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多