AVPlayer 无法播放

发布于 2024-11-26 11:52:21 字数 281 浏览 0 评论 0原文

我想用 AVPlayer 播放远程 mp3。我无法让它工作,我也看不出它不起作用的原因。代码:

NSString *urlstr=@"..some link to a mp3"
NSURL *url=[NSURL URLWithString:urlstr];
self.player = [[[AVPlayer alloc] initWithURL:url] retain];
[player play];

链接有效。如果我将其加载到 webView 中,它会立即播放。 请帮忙。

I want to play a remote mp3 with AVPlayer. I can't make it work and I can't see the reason why it doesn't work. Code:

NSString *urlstr=@"..some link to a mp3"
NSURL *url=[NSURL URLWithString:urlstr];
self.player = [[[AVPlayer alloc] initWithURL:url] retain];
[player play];

The link is valid. If I load it in a webView it plays right away.
Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

机场等船 2024-12-03 11:52:21

我已经找到解决办法了。请尝试以下代码:

NSString *urlstr=@"http://www.3rdeyelab.com/mp3/mp3/tailtoddle_lo.mp3"
NSURL *url=[NSURL URLWithString:urlstr];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
player = [[AVPlayer playerWithPlayerItem:playerItem] retain];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

希望它对您有用!

I have get a solution. Please try the following code:

NSString *urlstr=@"http://www.3rdeyelab.com/mp3/mp3/tailtoddle_lo.mp3"
NSURL *url=[NSURL URLWithString:urlstr];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
player = [[AVPlayer playerWithPlayerItem:playerItem] retain];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

Hope it will work for you!!!

呆头 2024-12-03 11:52:21

如果您使用本地链接作为 URL,那么您应该使用 fileURLWithPath 方法启动 url:

NSURL *url = [NSURL fileURLWithPath:urlstr];

If you use a local link as URL, so you should initiate url with fileURLWithPath method:

NSURL *url = [NSURL fileURLWithPath:urlstr];
弥枳 2024-12-03 11:52:21

无论 URL 是本地的还是远程的,只要涉及 URL,就应该始终使用 NSURLConnection 类。这是加载 AVPlayerItem 或 AVAsset 的唯一线程安全、非侵入式方法。请记住:您的应用程序与其他应用程序共享 iPhone;而且,这才是负责任的做法。

The NSURLConnection class, regardless of whether the URL is local or remote, should always be used whenever a URL is concerned. It's the only thread-safe, non-intrusive means of loading a AVPlayerItem or AVAsset. Remember: your app shares the iPhone with other apps; and, this is the way to be responsible insodoing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文