2010-11-25 53 views

回答

10

这里是一个替换码样本,将所述文件作为AAC编码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]; 

然后结束我所用的记录:

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

然后,在'tmpFileUrl'的文件可以是用过的。

+0

您可能需要等待`audioRecorderDidFinishRecording:successfully:`来确定文件是否有效。 – alltom 2013-03-05 11:04:42

+1

太好了,这可以直接记录到* m4a,而不必稍后转换。 您应该使用AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation而不是AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation,因为它自4.0以来已被弃用(仍然有效,但以防万一)。 – DZenBot 2013-06-09 19:11:44

3

这是用于录制m4a音频文件的工作SWIFT代码。请记住,在iOS中产生可用音频文件的正确格式的paframeter是非常痛苦的发现。经过多次试验和错误,我发现了这种组合。我希望它能节省您的时间,享受!

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 
    }