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

但是我想以.m4a格式录制。

请帮助我这样做。

谢谢。

有帮助吗?

解决方案

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

其他提示

这是录制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
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top