录制音频并永久保存在iOS中

es1*_*es1 5 xcode record objective-c ios

我制作了2个iPhone应用程序,可以录制音频并将其保存到文件中并再次播放.

其中一个使用AVAudiorecorder和AVAudioplayer.第二个是Apple的SpeakHere音频队列示例.

两者都运行在Simulater和Device上.

但是,当我重新启动任一应用程序时,找不到录制的文件!我已经尝试了在stackoverflow上找到的所有可能的建议,但它仍然无法正常工作!

这是我用来保存文件的方法:

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound1.caf"]; 
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
Run Code Online (Sandbox Code Playgroud)

es1*_*es1 15

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

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

    sound1.caf

  3. 用一个新的.同名但没有内容.

  4. 播放只会播放一个空的新文件. - >显然没有声音.


所以这就是我所做的:

我使用NSUserdefaults来保存稍后在我的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];
}
Run Code Online (Sandbox Code Playgroud)

ViewController.m中编辑的记录:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

在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];


}
Run Code Online (Sandbox Code Playgroud)

并为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"];
}
Run Code Online (Sandbox Code Playgroud)

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

    //Load recording path from preferences
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


temporaryRecFile = [prefs URLForKey:@"Test1"];



player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];
Run Code Online (Sandbox Code Playgroud)

在(IBAction)playBack.temporaryRecFile是我的ViewController类中的NSURL变量.

声明如下ViewController.h:

@interface SoundRecViewController : UIViewController <AVAudioSessionDelegate,AVAudioRecorderDelegate, AVAudioPlayerDelegate>
{
......
......
    NSURL *temporaryRecFile;

    AVAudioRecorder *recorder;
    AVAudioPlayer *player;

}
......
......
@end
Run Code Online (Sandbox Code Playgroud)

  • @ user2105811你先生是一个传奇.我一直试图让这个功能在我的应用程序中工作多年,但我不能:(非常感谢你,非常有帮助. (2认同)
  • 非常感谢.我浪费了三天时间寻找问题的原因.我做了很多无用的测试,以了解为什么录音被删除. (2认同)
  • 事实是,很难想象创建记录器[AVAudioPlayer alloc] initWithContentsOfURL会删除前一个文件的内容.难以置信的!我失去了超过3天试图了解发生了什么!再次感谢你! (2认同)