通过麦克风iOS录制音频,并使用顶部红色栏进行操作

时间:2015-01-19 12:17:37

标签: ios objective-c audio-recording avaudiorecorder avaudiosession

我试图通过麦克风录制音频。我正在使用AVAudioRecorder。录音效果很好,但存在问题。录制开始后我看不到顶部的红色条。录音停止后,我甚至看不到它消失了。但是,当我停止录制并尝试popViewController:animated顶部红条出现并在同一时间消失。在此之后我的视图向上滚动。

这是我在viewDidLoad:

中的代码
NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               @"TempRecord.m4a",
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryRecord error:nil];
    // Define the recorder setting
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];


    // Initiate and prepare the recorder
    NSError *error;
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:&error];
    [recorder setDelegate:self];
    [recorder setMeteringEnabled:YES];

    if(![recorder prepareToRecord])NSLog(@"Error preparing");

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        //
        if ([session respondsToSelector:@selector(requestRecordPermission:)]) {
            [session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
                if (granted) {
                    // Microphone enabled code
                    NSLog(@"Microphone is enabled..");
                    [self performSelector:@selector(startRecording) withObject:nil afterDelay:0.3];
                }
                else {
                    // Microphone disabled code
                    NSLog(@"Microphone is disabled..");
                    [_recordButton setEnabled:NO];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        microphoneDisabledAllert =[[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                                     message:@"This app requires access to your device's Microphone.\n\nPlease enable Microphone access for this app in Settings / Privacy / Microphone"
                                                    delegate:nil
                                           cancelButtonTitle:@"Dismiss"
                                           otherButtonTitles:nil];
                        [microphoneDisabledAllert setDelegate:self];
                        [microphoneDisabledAllert show];
                    });
                }
            }];
        }
    }
}

这是我控制录音的两种方法

-(void)startRecording{
    NSError* error;
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:&error];
    if (error) NSLog(@"Error start:[%@]",[error localizedDescription]);

    [recorder record];

    recordTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
}
- (IBAction)stopRecording:(id)sender {
    [recorder stop];

    [recordTimer invalidate];
    recordTimer=nil;
    NSError* error;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive:NO error:&error];
    if(error)NSLog(@"Error stop:[%@]",[error localizedDescription]);
    [self.navigationController popViewControllerAnimated:YES];
}

修改 目前我正在使用iOS 8,并且控制台中没有错误。

0 个答案:

没有答案