如何从 JSON 输出播放音频文件?
How to play an audio file from JSON output?
我有一个 iOS 应用程序,它以 UITableView 的形式输出 JSON 信息(来自 mySQL 数据库)。这包括音频、图像和文本。虽然我可以调用图像和文本,但我无法调用音频(这在 mySQL 数据库中存储为 URL)例如,这是 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'
音频存储在我的服务器上,因此它不包含在应用程序包中。这是否意味着我需要将文件下载到我的应用程序中?或者还有其他方法吗?有人知道我做错了什么吗?任何帮助表示赞赏。
我想通了。这就是我定位图像路径的方式:
NSURL *url = [NSURL URLWithString:[_wordDetail objectForKey:@"audiopath"]];
NSData *soundData = [NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData error:NULL];
audioPlayer.delegate = self;
[audioPlayer play];
我有一个 iOS 应用程序,它以 UITableView 的形式输出 JSON 信息(来自 mySQL 数据库)。这包括音频、图像和文本。虽然我可以调用图像和文本,但我无法调用音频(这在 mySQL 数据库中存储为 URL)例如,这是 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'
音频存储在我的服务器上,因此它不包含在应用程序包中。这是否意味着我需要将文件下载到我的应用程序中?或者还有其他方法吗?有人知道我做错了什么吗?任何帮助表示赞赏。
我想通了。这就是我定位图像路径的方式:
NSURL *url = [NSURL URLWithString:[_wordDetail objectForKey:@"audiopath"]];
NSData *soundData = [NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData error:NULL];
audioPlayer.delegate = self;
[audioPlayer play];