0
我试图创建一个应用程序,允许某人在iOS5中永久录制和保存其声音。该录音随后可以随时播放。我的代码只要用户停留在记录和播放音频的视图上,但当我离开视图时,回到它并尝试播放音频时,出现此错误:离开视图后无法播放AVAudio播放器与AVAudio播放器
错误:操作无法完成。 (OSStatus错误-50。)
显然,离开视图导致保存在Documents文件夹中的文件无法工作?有没有人有解决方案或回答我如何解决这个问题?
我的代码录制和播放音频低于:
- (IBAction)playSound:(id)sender {
if (!self.audioRecorder.recording)
{
NSError *error;
if (self.audioPlayer)
self.audioPlayer = nil;
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:self.audioRecorder.url
error:&error];
self.audioPlayer.delegate = self;
[self.audioPlayer prepareToPlay];
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[self.audioPlayer play];
}
}
- (IBAction)recordSound:(id)sender {
if(!self.audioRecorder.recording)
{
self.dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
self.docsDir = [self.dirPaths objectAtIndex:0];
self.soundFilePath = [self.docsDir
stringByAppendingPathComponent:@"sound.caf"];
self.soundFileURL = [NSURL fileURLWithPath:self.soundFilePath];
self.recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
self.audioRecorder = [[AVAudioRecorder alloc]
initWithURL:self.soundFileURL
settings:self.recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[self.audioRecorder prepareToRecord];
}
[self.audioRecorder record];
[self.audioRecorder recordForDuration:(NSTimeInterval) 5];
[self.record setTitle:@"Stop" forState: UIControlStateNormal];
}
else if (self.audioRecorder.recording)
{
[self.audioRecorder stop];
[self.record setTitle:@"Record" forState: UIControlStateNormal];
}
else if (self.audioPlayer.playing) {
[self.audioPlayer stop];
}
}
我已经用AVAudioRecorder来做soundFilePath等。另外,在audioPlayer上面添加说明不起作用。声音文件(或它的一些外表)仍然存在,因为我得到这个错误,所以显然它正在读取一些东西。它只是无法播放该文件。 – user1591483 2012-08-13 14:58:46
哦,等一下,没关系。它现在有效。 – user1591483 2012-08-13 15:01:40
如果有效,请考虑接受答案。也是一个投票不会伤害:) – 2012-08-13 16:06:19