gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:04:24 25 4
gpt4 key购买 nike

我有一个 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'

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

最佳答案

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

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

关于mysql - 如何从 JSON 输出播放音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31791973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com