克服锁定屏幕+静音模式播放音频

时间:2013-03-20 11:44:55

标签: iphone ios audio notifications

This video是一个可以

的应用录音
  • 覆盖无声模式
  • 过度锁定屏幕
  • 播放闹钟音乐/旋律

我们的团队正在开发类似的定制iPhone闹钟

如果有人可以提供帮助,我将非常感激。我一直试图打破这个两个星期。

当设备被锁定时,不会调用我们的下面方法。这就是警报声没有播放的原因

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    
    if ([GlobalData gSettings].vibration) {
        timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        isVibration = YES;
    } else {
        isVibration = NO;
    }
    
    self.uinfo = notification.userInfo;
    
    
    
    NSString *soundname = [uinfo objectForKey:@"sound"];
    NSURL *clip = [[NSBundle mainBundle] URLForResource:soundname withExtension:@"caf"];
    if (clip) {
        
        self.avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
        self.avPlayer.delegate = self;
        AudioSessionInitialize (NULL, NULL, NULL, NULL);
        AudioSessionSetActive(true);
        
        // Allow playback even if Ring/Silent switch is on mute
        UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
        AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
                                 sizeof(sessionCategory),&sessionCategory);
    }
    else
    {
        
        NSURL *clip = [[NSUserDefaults standardUserDefaults]URLForKey:[uinfo objectForKey:@"sound"]];
        self.avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
        self.avPlayer.delegate = self;
        AudioSessionInitialize (NULL, NULL, NULL, NULL);
        AudioSessionSetActive(true);
        
        // Allow playback even if Ring/Silent switch is on mute
        UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
        AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
                                 sizeof(sessionCategory),&sessionCategory);
    }
    

    [self.avPlayer play];

1 个答案:

答案 0 :(得分:1)

我稍微挖了一下LivingEarth应用程序。它们似乎正在使用基于MMDeepSleepPreventer的组件。他们使用某种内部警报系统(播放的音乐不是由本地通知触发,而是由NSTimer触发)。

然而,我发现它在较新的iOS版本中不可靠,所以我稍微调整了一下。启动时,它会继续播放静音音频文件,即使用户按下锁定按钮,也可以让应用程序运行NSTimers

你可以找到我的前叉here

相关问题