我想播放从服务器检索到的声音文件,作为 base 64 编码文件。
到目前为止,我已经使用 base64 解码了该文件并将其写入文件,如下所示:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"voicemail.wav"]];
[fileManager createFileAtPath:filepath contents:[responseDict objectForKey:@"ResponseData"] attributes:nil];
但是,我在实际播放声音文件时遇到问题。我的代码返回错误:
无法完成操作。OSStatus 错误2003334207。
你能告诉我哪里出错了吗?
NSURL * url = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *sound = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err] autorelease];
if (! sound) {
NSLog(@"Sound had error %@", [err localizedDescription]);
} else {
[sound prepareToPlay];
[sound play];
}
@Himanshu
正如Mattias Wadman所建议的那样,我尝试使用vlc/iTunes媒体播放器播放下载的文件,但文件无法播放。因此,问题可能出在文件的编码或解码中,而不是在播放音频代码中。我检查了相同的内容,并且在编码时嵌入了一些额外的字符。我们进行了编码并解决了这个问题。
希望这有帮助。
NSData *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"FileName.mp3"]
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
AVAudioPlayer *sound = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err] autorelease];
if (! sound) {
NSLog(@"Sound had error %@", [err localizedDescription]);
} else {
[sound prepareToPlay];
[sound play];
}