1
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 文件中设置记录器委托,但没有调用此方法为什么会发生这种情况。

帮我做这件事。

提前致谢。

4

2 回答 2

0

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

于 2013-10-23T13:00:35.283 回答
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

于 2013-10-23T14:20:57.263 回答