录制audio并永久保存在iOS中

我已经做了2个iPhone应用程序,可以录制audio并将其保存到文件并再次播放。

其中一个使用AVAudiorecorder和AVAudioplayer。 第二个是苹果公司的SpeakHereaudio队列的例子。

既可以在Simulater上运行,也可以在Device上运行。

但当我重新启动任何应用程序logging的文件没有find! 我已经尝试了所有可能的build议在stackoverflow上find,但它仍然无法正常工作!

这是我用来保存文件:

NSArray *dirPaths; NSString *docsDir; dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound1.caf"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

好吧,我终于解决了。 问题是我正在设置AVAudioRecorder,并在我的ViewController.m的viewLoad中覆盖具有相同名称的现有文件的path。

  1. 录制并保存audio到文件并停止应用程序后,我可以在Finder中find该文件。 (/ Users / xxxxx / Library / Application Support / iPhone Simulator / 6.0 / Applications / 0F107E80-27E3-4F7C-AB07-9465B575EDAB / Documents / sound1.caf)
  2. 当我重新启动应用程序时,logging器的设置代码(来自viewLoad)只会覆盖我的旧文件,名为:

    sound1.caf

  3. 一个新的。 同名但不含内容。

  4. 回放只会播放一个空的新文件。 – >没有明显的声音。


所以这就是我所做的:

我用NSUserdefaults来保存logging文件名的path,稍后在我的playBack方法中检索。


清理ViewController.m中的viewLoad:

 - (void)viewDidLoad { AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; [audioSession setActive:YES error:nil]; [recorder setDelegate:self]; [super viewDidLoad]; } 

在ViewController.m中编辑logging:

 - (IBAction) record { NSError *error; // Recording settings NSMutableDictionary *settings = [NSMutableDictionary dictionary]; [settings setValue: [NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; [settings setValue: [NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey]; [settings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey]; [settings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; [settings setValue: [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey]; NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath_ = [searchPaths objectAtIndex: 0]; NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[self dateString]]; // File URL NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH]; //Save recording path to preferences NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setURL:url forKey:@"Test1"]; [prefs synchronize]; // Create recorder recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error]; [recorder prepareToRecord]; [recorder record]; } 

在ViewController.m中编辑回放:

 -(IBAction)playBack { AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; [audioSession setActive:YES error:nil]; //Load recording path from preferences NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; temporaryRecFile = [prefs URLForKey:@"Test1"]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil]; player.delegate = self; [player setNumberOfLoops:0]; player.volume = 1; [player prepareToPlay]; [player play]; } 

并为ViewController.m添加了一个新的dateString方法:

 - (NSString *) dateString { // return a formatted string for a file name NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"ddMMMYY_hhmmssa"; return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".aif"]; } 

现在它可以通过NSUserdefaults加载最后logging的文件:

  //Load recording path from preferences NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; temporaryRecFile = [prefs URLForKey:@"Test1"]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil]; 

在(IBAction)playBack中。 temporaryRecFile是我的ViewController类中的一个NSURLvariables。

声明如下ViewController.h:

 @interface SoundRecViewController : UIViewController <AVAudioSessionDelegate,AVAudioRecorderDelegate, AVAudioPlayerDelegate> { ...... ...... NSURL *temporaryRecFile; AVAudioRecorder *recorder; AVAudioPlayer *player; } ...... ...... @end