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

これを試して

-(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 に答える