0

我正在该应用程序中开发一个应用程序,我使用了 AudioRecorder。当应用程序关闭时,我的录音机不会停止。当我的应用程序处于后台时,会显示红色状态栏。

使用此代码...

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

        // Setup audio session
        AVAudioSession *session = [AVAudioSession sharedInstance];
        //[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

        [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
        delegate.recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];

        delegate.recorder.delegate=self;

        delegate.recorder.meteringEnabled = YES;
        [delegate.recorder prepareToRecord];


        //[recorder record];  // Do not start recording here

        [delegate.recorder updateMeters];
        level = [delegate.recorder peakPowerForChannel:0];

        //AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        //[audioSession setActive:NO error:nil];


        timer = [NSTimer scheduledTimerWithTimeInterval:0.2  target:self
                                               selector:@selector(updateAudioMeter)
                                               userInfo:nil
                                                repeats:YES];



-(void)updateAudioMeter
{
    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    [delegate.recorder updateMeters];


    level = [delegate.recorder averagePowerForChannel:0];

    NSLog(@"%f",level);

    if(level<=-120)
    {
        [delegate.recorder record];
    }
    else
    {
        [delegate.recorder stop];
    }
}


- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [recorder stop];
}

但是当我的应用程序关闭时,红色状态栏显示意味着我的 iPhone5 没有停止录制。我哪里错了,请帮我解决这个问题。

4

0 回答 0