如何从JSON输出播放音频文件?

时间:2015-08-03 16:09:29

标签: mysql avaudioplayer

我有一个iOS应用程序,以UITableView的形式输出JSON信息(来自mySQL数据库)。这包括音频,图像和文本。虽然我可以调用图像和文本,但是我无法调用音频(这是作为URL存储在mySQL数据库中),例如,这是我的音频文件路径的JSON输出:

[{"audiopath = "http://my-website.com/audio/5552643-audio.caf"}]

这是我用来调用图像和文本的代码,但音频不起作用。我已经尝试记录路径,但它只是为NULL。

NSDictionary *words = [self.wordsArray objectAtIndex:index];
label.text = [words objectForKey:@"new"];

NSDictionary *images = [self.thumbArray objectAtIndex:index];
[imgView setImageWithURL:[NSURL URLWithString:[images objectForKey:@"thumbnail"]]];

NSDictionary *audio = [self.audioArray objectAtIndex:index];
NSURL *path = [NSURL URLWithString:[audio objectForKey:@"audiopath"]];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];
NSLog(@"PATH:%@", path);

我得到的错误是:

'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

音频存储在我的服务器上,因此它不包含在应用程序包中。这是否意味着我需要将文件下载到我的应用程序中?还是有另一种方式?谁知道我做错了什么?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

我已经弄清楚了。这就是我如何定位图像路径:

NSURL *url = [NSURL URLWithString:[_wordDetail objectForKey:@"audiopath"]];
NSData *soundData = [NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData  error:NULL];
audioPlayer.delegate = self; 
[audioPlayer play];