AVPlayer 无法播放
我想用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经找到解决办法了。请尝试以下代码:
希望它对您有用!
I have get a solution. Please try the following code:
Hope it will work for you!!!
如果您使用本地链接作为 URL,那么您应该使用
fileURLWithPath
方法启动 url:If you use a local link as URL, so you should initiate url with
fileURLWithPath
method:无论 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.