2013-02-25 111 views
4

我製作了2個iPhone應用程序,可以錄製音頻並將其保存到文件並再次播放。錄製音頻並永久保存在iOS中

其中之一使用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]; 
+0

哪裏(哪個目錄),你存儲的文件? – 2013-02-25 02:14:05

+0

Documents目錄是這樣的: – es1 2013-02-25 02:19:03

+0

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]; – es1 2013-02-25 02:19:21

回答

14

好吧,我終於解決了這個問題。問題在於我設置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的用於保存要在我的播放方法後檢索到的記錄文件名的路徑。


清洗viewLoad在ViewController.m:

- (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添加了一個新的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 
+1

這個答案几乎是完美的 - 直到我設置音頻記錄模式才能記錄您的記錄方法時,它才奏效 - 但後來它在我的設備上完美運行 – 2014-02-24 17:10:59

+2

@ user2105811您的先生是一位傳奇人物。我一直試圖讓這個功能在我的應用程序中工作很久,但我不能:(謝謝你這麼多人,真的很有幫助。 – Supertecnoboff 2015-02-16 07:23:53

+2

非常感謝,我浪費了三天的時間來尋找問題的原因。我做了很多無用的測試,以瞭解爲什麼錄音被擦除 – Markus 2016-04-22 22:05:41