未调用记录器委托方法

Posted

技术标签:

【中文标题】未调用记录器委托方法【英文标题】:Recorder delegate method not called 【发布时间】:2013-10-23 12:52:53 【问题描述】:
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 文件中设置记录器委托,但没有调用此方法为什么会发生这种情况。

帮我做这件事。

提前致谢。

【问题讨论】:

使用-audioRecorderEncodeErrorDidOccur:error: 也可以识别录制过程中的任何编码错误。 【参考方案1】:

您实际上是否将AVAudioRecorder 实例存储在某处?如果你不是,那么它很可能在上述代码完成时被释放。

【讨论】:

【参考方案2】:

试试这个

-(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

【讨论】:

以上是关于未调用记录器委托方法的主要内容,如果未能解决你的问题,请参考以下文章

UIPopoverController 委托委托方法未被调用

UIDocumentPickerViewController - 未调用委托方法

未调用委托方法

未调用 SWTableViewCell 委托方法

AVAudioPlayer 和 AVAudioRecorder:未调用委托方法

未调用委托方法