好吧,我終於解決了這個問題。問題在於我設置AVAudioRecorder,並在我的ViewController.m的viewLoad中覆蓋路徑,覆蓋具有相同名稱的現有文件。
- 錄製並保存音頻到文件並停止應用後,我可以在Finder中找到該文件。 (/ Users/xxxxx/Library/Application Support/iPhone Simulator/6.0/Applications/0F107E80-27E3-4F7C-AB07-9465B575EDAB/Documents/sound1.caf)
當我重新啓動應用程序時,從viewLoad)將只是簡單地覆蓋我的所謂的舊文件:
sound1.caf
有一個新的。同名但不含內容。
回放將播放一個空的新文件。 - >明顯沒有聲音。
因此,這裏是我做過什麼:
我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
來源
2013-02-25 23:49:33
es1
哪裏(哪個目錄),你存儲的文件? – 2013-02-25 02:14:05
Documents目錄是這樣的: – es1 2013-02-25 02:19:03
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