2012-02-20 48 views
2

在我的應用程序中,我希望用戶能夠錄製一個聲音文件並播放它,然後保存聲音文件以備後用。我使用this tutorial來設置播放和錄製界面,但本教程沒有說明一個關鍵部分:如何將聲音永久保存到磁盤?保存錄制的AVAudioRecorder聲音文件:現在怎麼辦? (iOS,Xcode 4)

此外,當你在這裏時,如何設置聲音文件記錄的最大時間?我不想要超過30秒的聲音文件。

謝謝,希望我能把這一切都整理出來。

+3

-1'ed爲「蹩腳的教程鏈接」 - 它缺乏事實你想要的東西,並不會讓它變得糟糕,尤其是因爲它顯而易見已經有了多少工作。 (我與該教程沒有關係)。 – 2012-07-09 13:20:38

回答

1

Voice Record Demo是一個很好的例子來看看。它使用的Cocos2D的UI,但如果你只需要看看HelloWorldScene.m中的類,它包含了所有你需要的代碼:

  1. 創建一個新的音頻會話
  2. 檢查麥克風連接
  3. 開始錄製
  4. 停止錄製
  5. 保存錄制的音頻文件
  6. 播放保存語音文件

後您初始化音頻會議,你可以使用一個方法,如下面的一個方式的記錄保存到指定的文件名:

-(void) saveAudioFileNamed:(NSString *)filename { 

destinationString = [[self documentsPath] stringByAppendingPathComponent:filename]; 
NSLog(@"%@", destinationString); 
NSURL *destinationURL = [NSURL fileURLWithPath: destinationString]; 

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithFloat: 44100.0],     AVSampleRateKey, 
          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, 
          [NSNumber numberWithInt: 1],       AVNumberOfChannelsKey, 
          [NSNumber numberWithInt: AVAudioQualityMax],   AVEncoderAudioQualityKey, 
          nil]; 

NSError *error; 

recorder = [[AVAudioRecorder alloc] initWithURL:destinationURL settings:settings error:&error]; 
recorder.delegate = self; 
} 
相關問題