我瀏覽了與該主題相關的各種帖子,但答案並沒有真正的幫助。我用this教程來實現音頻文件的錄製和播放。似乎缺少的是如何永久保存記錄。當我退出我的應用程序時,聲音文件就在那裏,但沒有任何內容。我甚至不知道它是保存rocerd還是隻創建文件。 下面是一個代碼示例:xcode:如何使用AVFoundation錄製音頻後保存音頻文件
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];
我看到一個可能的解決了這個問題here但因爲我是新來的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];
}
}
這是我的播放方法:
-(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");
}
}
}
這是我明白我做了什麼。 任何建議,我可能是錯的?
你好@mahboudz,我用[記錄器停止]停止記錄。我不認爲這是問題。停止錄製後,我可以使用與錄音機具有相同網址的AVplayer播放「已錄製」聲音。問題是,我重新啓動我的應用程序後聲音沒有播放。 Taht表示它不是永久保存的,但僅在會話/實例期間保存。 – iPhoneNoob 2012-02-22 00:20:04
您是否在停止錄製後說文件長度爲零? – mahboudz 2012-02-22 00:48:59
您是否實現了委託方法:' - (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)記錄器錯誤:(NSError *)錯誤'?這可以告訴你加密時是否有錯誤。你什麼時候叫'-stop'?它是在什麼時候將應用程序放入後臺?或之前? – mahboudz 2012-02-22 00:50:28