誰かが自分の声を録音して永久に保存できるアプリケーションを iOS5 で作成しようとしています。その録音はいつでも再生できます。私のコードは、ユーザーがオーディオを録音して再生するビューにとどまっている限り機能しますが、ビューを離れて戻ってきてオーディオを再生しようとすると、次のエラーが発生します。
エラー: 操作を完了できませんでした。(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];
}
}