2012-09-15 34 views
12

私はオーディオ録音を実装しています。それはcaf & wave形式で正常に動作します。しかし問題は、ファイルサイズが大きすぎることです。iOS:オーディオ録音ファイル形式

だから、誰でも、ウィンドウで再生されるフォーマットの録音オーディオを手伝うことができます&ファイルサイズは少し小さいです。

私が試したコードは以下である:

dirPaths = NSSearchPathForDirectoriesInDomains(
                NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0]; 
    NSString *soundFilePath = [docsDir 
           stringByAppendingPathComponent:@"sound.wave"]; 
    NSLog(@"%@",soundFilePath); 
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

    NSDictionary *recordSettings = [NSDictionary 
         dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithInt:AVAudioQualityMin], 
            AVEncoderAudioQualityKey, 
            [NSNumber numberWithInt:16], 
            AVEncoderBitRateKey, 
            [NSNumber numberWithInt: 2], 
            AVNumberOfChannelsKey, 
            [NSNumber numberWithFloat:8000.0], AVSampleRateKey, 
            [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey, 
            nil]; 

    NSError *error = nil; 

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

    if (error) 
    { 
     NSLog(@"error: %@", [error localizedDescription]); 

    } else { 
     [audioRecorder prepareToRecord]; 
    } 
+0

http://stackoverflow.com/questions/4279311/how-to-record-voice-in-m4a-formatの複製? – nneonneo

+0

m4a(MPEG-4 AAC)形式のレコード。詳細については、http://stackoverflow.com/questions/4279311/how-to-record-voice-in-m4a-formatを参照してください。 – nneonneo

答えて

16

Iも.M4Aファイルに好ましいフォーマットをエンコードされたAACオーディオ記録するAVAudioRecorderを使用しようとしました。コアオーディオファイル(.caf)の内部にAACに録音するコードをすぐに手に入れることができましたが、AVAudioRecorderで.m4aを適切にフォーマットすることができませんでした。私はアプリケーションの録音コードを低レベルのAudio Units APIを使って書き直そうとしていましたが、代わりにバックバーナーに入れて共有オーディオセッションを処理するコードを追加しました。それが設定されたら、私は戻って.m4aとして保存しようとしたところ、ちょうどうまくいった。そして、「tmpFileUrl」でのファイルは、あなたのようにしてください使用することができます

[recorder stop]; 
AVAudioSession *session = [AVAudioSession sharedInstance]; 
int flags = AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation; 
[session setActive:NO withFlags:flags error:nil]; 

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docsDir = [dirPaths objectAtIndex:0]; 
NSURL *tmpFileUrl = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"tmp.m4a"]]; 
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, 
         [NSNumber numberWithFloat:16000.0], AVSampleRateKey, 
         [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, 
         nil]; 
NSError *error = nil; 
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error]; 
[recorder prepareToRecord]; 

AVAudioSession *session = [AVAudioSession sharedInstance]; 
[session setCategory:AVAudioSessionCategoryRecord error:nil]; 
[session setActive:YES error:nil]; 

[recorder record]; 

その後、私は使用した記録を終了するには:ここで

は、いくつかのサンプルコードです。ここで

+0

'setActive:withFlags:error:'はiOS 6の後で廃止されました。代わりに 'setActive:error:'(フラグを設定せずに)を使うことができます。それは私のためにうまく動作します。 – Vineeth

+0

Dude!保存された私の日:)私は彼女の1歳の誕生日のために私の赤ちゃんのためにアプリを作っていますので、彼女はありがとうございます。<3 –

+0

kAudioFormatMPEG4AACからkAudioFormatMPEG4AACに変更すると1.3MBから45kbにファイルサイズが変更されます – Thiru

1
NSMutableDictionary *settings = [[NSMutableDictionary alloc] initWithCapacity:0]; 

[settings setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; 
[settings setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey]; 
[settings setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey]; 
[settings setValue :[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey]; 
[settings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 
[settings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; 

//Encoder 

[settings setValue :[NSNumber numberWithInt:12000] forKey:AVEncoderBitRateKey]; 
[settings setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitDepthHintKey]; 
[settings setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitRatePerChannelKey]; 
[settings setValue :AVAudioQualityMin   forKey:AVEncoderAudioQualityKey]; 
6

は(バランスに)高品質と小さいファイルサイズのための私の設定です:

NSDictionary *recordSettings = @{AVEncoderAudioQualityKey: @(AVAudioQualityMedium), 
           AVFormatIDKey: @(kAudioFormatMPEG4AAC), 
           AVEncoderBitRateKey: @(128000), 
           AVNumberOfChannelsKey: @(1), 
           AVSampleRateKey: @(44100)}; 

これは、あなたのCD品質のモノラルAACトラックの近くを与えます。あなたはどこでも読めるように.m4aでファイルを追加するべきです。他のすべてのパラメータは、デバイスのデフォルトに設定されます。これは、ほとんどの場合に起こりたいことです。

+0

もう一度、これは正常に動作するために使用されましたが、かなりのレベルがあれば、現在はiOS10で歪んでいます。 iPad Proを使用する –

関連する問題