没有调用记录器委托方法

时间:2013-10-23 12:52:53

标签: ios iphone objective-c ios7

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];

    // 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
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];

    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    //Start the actual Recording
    [recorder record];

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

    NSLog(@"%f",level);

我在viewDidload()方法中使用此代码来录制声音级别。

我想在设备获得声音时调用deleget方法。

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{


    NSLog(@"Level is %f",level);


}

此方法未调用。

我在.h文件中设置录制器委托但未调用此方法为什么会发生这种情况。

帮助我做到这一点。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您实际上是在某个地方存储AVAudioRecorder实例吗?如果你不是那么它可能在上面的代码完成时被解除分配。

答案 1 :(得分:0)

试试这个

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

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

// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

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

// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;

recorder.meteringEnabled = YES;

[player setVolume: 1.0];

[recorder prepareToRecord];

}

 -(IBAction)record:(id)sender {


   if (!recorder.recording) {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    recordBtn.selected=YES;

    tabBtn.selected = YES;
    tabBtn.userInteractionEnabled = NO;

    // Start recording


    [recorder record];

   } else {


  }


 }


   -(void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{




recorder = nil;
player = nil;
spinnerView = nil;


UploadAudioViewController *upload = [[UploadAudioViewController alloc]initWithNibName:@"UploadAudioViewController" bundle:nil];


NSLog(@"recorder url----%@",recorderurl);

upload.audioURL = recorderurl;

[self.navigationController pushViewController:upload animated:YES];




 }

并确保 - 您使用了AVAudioRecorderDelegate和AVAudioPlayerDelegate