2010-11-25 10 views
4

既に録音するiPhoneアプリケーションを作成しました。それは.cafファイルに記録されます。音声を.m4a形式で録音する方法

しかし、私は.m4a形式で記録したいと思います。

どうかお手伝いください。

ありがとうございました。ここ

答えて

10

はM4A内部AACなどのファイルをコードする別のコードサンプルである:

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'にファイルすることができ中古。

+0

ファイルが有効かどうかを知るには、 'audioRecorderDidFinishRecording:successfully:'を待つことができます。 – alltom

+1

これは、後で変換することなく、* m4aに直接録音するのに最適です。 AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivationの代わりにAVAudioSessionSetActiveOptionNotifyOthersOnDeactivationを使用する必要があります。これは4.0以降で非推奨になっているためです(まだ動作しますが、その場合に備えて)。 – DZenBot

+0

このコードを実際に実行するために実行できるサンプルアプリケーションがありますか?私は、iOSデベロッパーセンターのサンプルレコーダーアプリにプラグインしようとしていましたが、私にとっては少し複雑です。ありがとう! –

3

ここでは、m4aオーディオファイルを記録するSWIFTコードを示します。 iOSで使用可能なオーディオファイルを生成する適切なフォーマットのパフメータを使用することは、本当に痛いことです。多くの試行錯誤の末、この組み合わせが見つかりました。私はそれがあなたに時間を節約し、楽しんでほしい!

let recordSettings: [String : AnyObject] = [AVSampleRateKey : NSNumber(float: Float(16000)), 
               AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)), 
     AVNumberOfChannelsKey : NSNumber(int: 1), 
     AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Low.rawValue))] 

func initializeAudioSession(){ 


    let audioSession = AVAudioSession.sharedInstance() 
    do { 
     try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord) 
     try audioRecorder = AVAudioRecorder(URL: self.directoryURL()!, 
              settings: recordSettings) 
     audioRecorder.delegate = self 
     audioRecorder.meteringEnabled = true 
     audioRecorder.prepareToRecord() 
    } catch let error as NSError{ 
     print("ERROR Initializing the AudioRecorder - "+error.description) 
    } 
} 

func recordSpeechM4A(){ 
     if !audioRecorder.recording { 
      let audioSession = AVAudioSession.sharedInstance() 
      do { 
       try audioSession.setActive(true) 
       audioRecorder.record() 
       print("RECORDING") 
      } catch { 
      } 
     } 
    } 

func directoryURL() -> NSURL { //filename helper method 
     let fileManager = NSFileManager.defaultManager() 
     let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 
     filepath = urls[0] 
     let documentDirectory = urls[0] as NSURL 
     print("STORAGE DIR: "+documentDirectory.description) 
     //print("---filepath: "+(filepath?.description)!) 
     let soundURL = documentDirectory.URLByAppendingPathComponent("recordedAudio.m4a") //.m4a 
     print("SAVING FILE: "+soundURL.description) 
     return soundURL 
    } 
+0

どのような形式で録音を保存しますか? .caf? .m4a?それはあなたのスニペットから不明です。 – Josh

+0

@Joshこのスニペットはm4a形式で保存されます。これは2つの場所に設定されています:1) 'recordSettings' kAudioFormatMPEG4AACパラメータで、2)self.directoryURL()から出てくる明示的な.m4a拡張子です。私は答えにdirectoryURL()のコードを追加しました。どこから来たのかを見ることができます。重要:何らかの理由で、AVSampleRateKeyとAVFormatIDKeyのrecordingSettingsの組み合わせが実際にクラッシュするため、動作するものに限定されます(私は試行錯誤で見つけました) – Josh

関連する問題