代码之家  ›  专栏  ›  技术社区  ›  jfalexvijay

如何录制.m4a格式的语音

  •  4
  • jfalexvijay  · 技术社区  · 14 年前

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

    但我想用.m4a格式录制。

    请帮我做这个。

    谢谢。

    2 回复  |  直到 10 年前
        1
  •  12
  •   Hemang    12 年前

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

        2
  •  5
  •   Josh    6 年前

    这是记录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
        }