13

オーディオ録音を実装しています。cafwaveフォーマットで正常に動作します。しかし、問題はファイルサイズが大きすぎることです。

だから、ウィンドウで再生され、ファイルサイズが少し小さいフォーマットでオーディオを録音するのを誰かが助けてくれますか?

私が試したコードは以下のとおりです。

 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];
     }
4

3 に答える 3

16

また、AVAudioRecorderを使用して、AACエンコード形式(できれば.m4aファイル)でオーディオを録音しようとしていました。コアオーディオファイル(.caf)内のAACに記録するコードをすばやく取得できましたが、AVAudioRecorderで.m4aを適切にフォーマットすることができませんでした。低レベルのAudioUnitsAPIを使用して、アプリケーションのレコーディングコードを書き直そうとしていましたが、代わりにそれをバックバーナーに配置し、共有オーディオセッションを処理するためのコードを追加しました。それが設定されたら、私は戻って.m4aとして保存しようとしましたが、うまくいきました。

次にいくつかのサンプルコードを示します。

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];

次に、使用した録音を終了します。

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

その後、「tmpFileUrl」のファイルを自由に使用できます。

于 2012-10-02T22:00:52.037 に答える
8

高品質でファイルサイズを小さくするための私の設定は次のとおりです(バランスが取れています)。

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

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

于 2013-07-01T13:27:50.220 に答える
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];
于 2012-09-15T11:48:28.313 に答える