gpt4 book ai didi

ios - 无法通过 AVAudioPlayer 中的 URL 播放声音

转载 作者:行者123 更新时间:2023-11-28 21:13:50 24 4
gpt4 key购买 nike

我查看并应用了 StackOverflow 上提供的几乎所有解决方案,但它对我不起作用。这是我的代码:

NSString *path = [NSString stringWithFormat:@"http://www.schillmania.com/projects/soundmanager2/demo/_mp3/rain.mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:path];

_player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];

[_player play];

根据类似问题中的其他答案,我也制作了 @property (strong, nonatomic) AVAudioPlayer *player; 但它仍然不起作用。我做错了什么?

根据评论我也试过这个:

NSURL *url = [[NSURL alloc]initWithString:@"http://www.schillmania.com/projects/soundmanager2/demo/_mp3/rain.mp3"];
AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:url];
AVPlayer *pla = [[AVPlayer alloc]initWithPlayerItem:item];
[pla play];

但还是没有播放!

最佳答案

正如@paiv 所说,AVAudioPlayer 不会从互联网上下载您的文件。 NSURL 用于本地文件和 Internet 资源,在这种情况下,声音必须是本地文件。您需要先下载文件,然后再播放。您需要下载文件(可能使用 NSURLSessionNSURLSessionDownloadTask),然后在文件下载完成后播放声音。

编辑:

正如@Sneak 在他下面的评论中指出的那样,AVPlayer 能够从网络流式传输音频,尽管实际上我自己从未使用过该功能。

关于ios - 无法通过 AVAudioPlayer 中的 URL 播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42073893/

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