簡體   English   中英

通過AVAudioRecorder錄制聲音

[英]Record the sound through AVAudioRecorder

我想錄制聲音,並想將錄制的文件保存到文檔目錄中:-已完成操作:-

[[AVAudioSession sharedInstance]
         setCategory: AVAudioSessionCategoryRecord
         error: nil];
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

        recorderFilePath = [[NSString stringWithFormat:@"%@/Audio.caf", DOCUMENTS_FOLDER] retain];
        NSURL *soundFileURL=[NSURL URLWithString:recorderFilePath];

    recorderFilePath = [[NSString stringWithFormat:@"%@/Audio.caf", DOCUMENTS_FOLDER] retain];

    NSURL *soundFileURL = [NSURL fileURLWithPath:recorderFilePath];


        NSLog(@"str==>%@ soundurl==>%@",recorderFilePath,soundFileURL);
        AVAudioRecorder *newRecorder =
        [[AVAudioRecorder alloc] initWithURL: url
                                    settings: settings
                                       error: nil];

 newRecorder.delegate = self;
        [newRecorder prepareToRecord];
        [newRecorder record];

和[記錄器停止]; 停止錄制時。

看過這個:- 這個

並進行了相應的編碼。.但是我仍然無法在文檔文件夾中創建任何.caf文件。

完成以下代碼后,可能會對您有所幫助。 首先將AVAudioRecorder的對象作為AVAudioRecorder * audioRecorder;

-(void)viewWillAppear:(BOOL)animated{

    NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    //here i took cache directory as saved directory you can also take   NSDocumentDirectory
    docsDir = [dirPaths objectAtIndex:0];

    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];//at this path your recorded audio will be saved

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

    NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:

                                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                                    AVEncoderAudioQualityKey,

                                                    [NSNumber numberWithInt:16], 
                                                    AVEncoderBitRateKey,

                                                    [NSNumber numberWithInt: 2], 
                                                    AVNumberOfChannelsKey,

                                                    [NSNumber numberWithFloat:44100.0], 
                                                    AVSampleRateKey,
                                                    nil];

    NSError *error = nil;
    audioRecorder = [[AVAudioRecorder alloc]initWithURL:soundFileURL settings:recordSettings error:&error];

    if( !audioRecorder ) {

        UIAlertView *alert  =
        [[UIAlertView alloc] initWithTitle: @"Warning" message: [error localizedDescription] delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        return;

    }

    if ( error )
    {
        NSLog( @"error: %@", [error localizedDescription] );
    }
    else {
        [audioRecorder prepareToRecord];
        [self recordAudio];//Mehod to Record Audio
    }

}

現在定義錄制音頻的方法,

 -(void) recordAudio
 {
     if (!audioRecorder.recording)
      {
       audioRecorder.delegate = self;
      [audioRecorder recordForDuration:10];//Here i took record duration as 10 secs 

     }

}

現在,您可以將AvAudio記錄DelegateMethods編寫為,

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:      (BOOL)flag
 {
  //Your code after sucessful recording;
 }
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
 {
NSLog(@"Encode Error occurred");
}

希望對您有幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM