如何以.m4a格式录制语音

时间:2010-11-25 16:53:59

标签: iphone avaudiorecorder m4a

我已经创建了一个iPhone应用程序来记录。它将记录在.caf文件中。

但我想以.m4a格式录制。

请帮我这样做。

感谢。

2 个答案:

答案 0 :(得分:11)

这是一个替代代码示例,它将文件编码为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'处的文件。

答案 1 :(得分:4)

以下是用于录制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
    }