[英]xcode: How to save audio file after recording audio using AVFoundation
我瀏覽了與該主題相關的各種帖子,但是答案並沒有真正的幫助。 我使用本教程來實現音頻文件的錄制和回放。 似乎缺少的是如何永久保存記錄。 當我退出應用程序時,聲音文件在那里,但是里面什么也沒有。 我什至不知道它是保存rocerd還是只是創建文件。 這是一個代碼示例:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"tmpSound.caf"];
tempRecFile = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
recorder = [[AVAudioRecorder alloc] initWithURL:tempRecFile settings:recSettings error:nil];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
我在這里看到了可能解決此問題的方法,但是由於我是iOS的新手,所以我沒有真正了解它,或者它對我不起作用。
請幫助並提供代碼示例。 我閱讀了有關AVFoundation和其他類的文檔,但沒有得到積極的結果。
SOS :)
UPDATE這是我的錄制方法:
-(IBAction)recording{
//***** METHOD for recording
if(isNotRecording){
isNotRecording = NO;
[recButton setTitle:@"Stop" forState:UIControlStateNormal];
recStateLabel.text = @"Recording";
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}
else{
isNotRecording = YES;
[recButton setTitle:@"Rec Begin" forState:UIControlStateNormal];
playButton.hidden = NO;
recStateLabel.text = @"Not recording";
[recorder stop];
}
}
這是我的viewDidLoad:
- (void)viewDidLoad
{
//record sound test code - start
isNotRecording = YES;
//playButton.hidden = YES;
recStateLabel.text = @"Not recording";
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
//record sound test code - END
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// code for generating the sound file that is to be saved
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
recorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[recorder prepareToRecord];
}
}
這是我的playBack方法:
-(IBAction)playback{
//AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:nil];
NSError *error;
// setting the uri of the formed created file
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL error:&error];
[player setNumberOfLoops:0];
[player prepareToPlay];
[player setVolume:1];
[player play];
NSLog(@"URL je: %@", soundFileURL);
//method 2
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
{
[player play];
if(player.isPlaying==YES)
{
NSLog(@"It's playing");
}
}
}
我希望這可以使我清楚地知道自己在做什么。 有什么建議我可能錯了嗎?
問題是當您將XCode(在viewDidLoad方法中)告訴[recorder prepareToRecord]時; 它擺脫了以前錄制的音頻文件。 消除“ if”語句中的那段代碼,當您退出並返回應用程序時,音頻文件將被保存。
您需要停止記錄才能將數據永久保存到文件中:
[recorder stop];
僅供參考-“ [recorder prepareToRecord]”方法創建一個新的音頻文件,覆蓋以前的所有已記錄文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.