0

サウンドを録音し、録音したファイルをドキュメントディレクトリに保存したい:-次のように実行しました:-

[[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ファイルを作成することはできません。

4

1 に答える 1

1

以下のコードを実行すると、役立つ場合があります。まず、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");
}

それがあなたに役立つことを願っています...

于 2013-03-14T10:13:40.533 に答える