app听起来如何工作?

时间:2014-01-23 11:53:15

标签: ios multitasking avaudiorecorder avaudiosession

我想制作连续录制背景声音的应用。像听到的应用就是这么做的。 但是我被中断了。应用程序在遇到中断时无法恢复录制。

我已将此用于通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAudioSessionInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:nil];

并恢复录制我有:

- (void)handleAudioSessionInterruption:(NSNotification*)notification {

    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];

    switch (interruptionType.unsignedIntegerValue) {
        case AVAudioSessionInterruptionTypeBegan:{
            // • Audio has stopped, already inactive
            // • Change state of UI, etc., to reflect non-playing state
            NSLog(@"Audio has stopped, already inactive");

            NSLog(@"is recording: %@",recorder.isRecording?@"yes":@"no");

            [self.recorder pause];

            NSLog(@"is recording after pause: %@",recorder.isRecording?@"yes":@"no");

            //[recorder pause];

        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            // • Make session active
            // • Update user interface
            // • AVAudioSessionInterruptionOptionShouldResume option




            if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
                // Here you should continue playback.


                //NSLog(@"completed");

                NSLog(@"start recording again %d");

                //NSLog(@"completed: %d",[recorder record]);

                // Set the audio file



                [self.recorder record];


                NSLog(@"is recording after again : %@",recorder.isRecording?@"yes":@"no");




                //[player play];
            }
        } break;
        default:
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

在plist中添加UIBackgroundModes和'音频'作为项目0.希望它会有所帮助 - 至少它在播放音频时适合我。

enter image description here