How to record voice in .m4a format

前端 未结 2 910
你的背包
你的背包 2020-12-30 15:28

Already I have created an iPhone application to record. It will record in .caf file.

But I want to record in .m4a format.

Please help me to do this.

相关标签:
2条回答
  • 2020-12-30 15:57

    Here is an alternative code sample that will encode the file as AAC inside an 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];
    

    Then to end the recording I used:

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

    Then the file at 'tmpFileUrl' can be used.

    0 讨论(0)
  • 2020-12-30 16:16

    Here is the working SWIFT code to record m4a Audio files. Bear in mind that hitting the right format parameters that produce usable audio files in iOS is really painful to find. I found that this combination works, after much trial and error. I hope it saves you time, enjoy!

    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 讨论(0)
提交回复
热议问题