Pregunta

Ya he creado una aplicación de iPhone para grabar. Se registrará en .caf archivo.

Pero yo quiero grabar en formato m4a.

Por favor me ayude a hacerlo.

Gracias.

¿Fue útil?

Solución

Este es un ejemplo de código alternativo que codificará el archivo como AAC dentro de un 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];

A continuación, para finalizar la grabación utilicé:

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

A continuación, el archivo en 'tmpFileUrl' se puede utilizar.

Otros consejos

Este es el código SWIFT de trabajo para grabar archivos de audio M4A. Tenga en cuenta que golpear a los parámetros de formato adecuados que producen archivos de audio que pueden utilizarse en iOS es muy doloroso para encontrar. He encontrado que esta combinación funciona, después de mucho ensayo y error. Espero que le ahorra tiempo, disfrutar!

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
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top