今天播放音频的时候遇到:Error Domain=NSOSStatusErrorDomain Code=2003334207 "The operation couldn’t be completed. (OSStatus error 2003334207.)"
原因:保存文件名并不是完整的路径,并重新创建URL或完整的路径,通过文件的路径(tmp)文件夹和文件名添加到它。
解决方法 :
NSData *audioData = [NSData dataWithContentsOfURL:[NSURL URLWithString:audioUrl]];
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray * arr = [audioUrl componentsSeparatedByString:@"/"];
NSLog(@"arr = %@",arr[arr.count - 1]);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirPath,arr[arr.count - 1]];
[audioData writeToFile:filePath atomically:NO];
NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
_player = [_player initWithContentsOfURL:fileURL error:&error];
if (_player == nil) {
NSLog(@"AudioPlayer did not load properly: %@", [error description]);
} else {
[_player play];
}
atomically:这个参数意思是如果为YES则保证文件的写入原子性,就是说会先创建一个临时文件,直到文件内容写入成功再导入到目标文件里.
如果为NO,则直接写入目标文件里.
1.录音结束后写如下代码,要不播放音频时声音会很小
AVAudioSession*session = [AVAudioSessionsharedInstance];
[sessionsetCategory:AVAudioSessionCategoryPlaybackerror:nil]; //此处需要恢复设置回放标志,否则会导致其它播放声音也会变小