【问题标题】:Record audio and save permanently in iOS在 iOS 中录制音频并永久保存
【发布时间】:2013-02-25 02:12:53
【问题描述】:

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

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

两者都在模拟器和设备上运行。

但是当我重新启动任一应用程序时,找不到录制的文件!!! 我已经尝试了在 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];

【问题讨论】:

  • 你在哪里(什么目录)存储文件??
  • Documents 目录如下:
  • NSArray *dirPaths; NSString *docsDir; dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:[globals sharedGlobals].folderCounter]; NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound1.caf"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
  • 您是否 NSLogged 文件路径字符串以确保它是您认为的那样?
  • 请编辑您的原始问题以添加您在评论中添加的代码。麻省理工学院几乎不可能以这种方式阅读,许多人会错过它作为评论。

标签: ios objective-c xcode record


【解决方案1】:

好的,我终于解决了。问题是我正在设置 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];
}

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

在 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 加载最后记录的文件了:

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


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



player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];

在(IBAction)播放中。 temporaryRecFile 是我的 ViewController 类中的一个 NSURL 变量。

声明如下 ViewController.h :

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

    AVAudioRecorder *recorder;
    AVAudioPlayer *player;

}
......
......
@end

【讨论】:

  • 这个答案几乎是完美的——直到我将音频会话模式设置为以你的录制方法录制之前它对我不起作用——但后来它在我的设备上完美运行
  • @user2105811 你先生是个传奇。多年来,我一直试图让这个功能在我的应用程序中工作,但我做不到:(非常感谢你,真的很有帮助。
  • 非常感谢。我浪费了三天时间寻找问题的原因。我做了很多无用的测试来了解为什么录音会被删除。
  • 事实是很难想象记录器的创建[AVAudioPlayer alloc] initWithContentsOfURL 擦除了之前文件的内容。逆天!我失去了超过 3 天的时间试图了解发生了什么!再次感谢您!
  • @AlvinfromDiaspar 因为一旦你终止应用并重新打开它,保存在变量中的值就会丢失。
猜你喜欢
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 2012-03-18
  • 2012-08-21
  • 1970-01-01
相关资源
最近更新 更多