xcode AVPlayer 无法播放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6818254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AVPlayer Can't Play
提问by Alex Tau
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:
我想用 AVPlayer 播放远程 mp3。我无法让它工作,我看不出它不工作的原因。代码:
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.
链接有效。如果我将它加载到 webView 中,它会立即播放。请帮忙。
回答by gypsicoder
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!!!
希望对你有用!!!
回答by Amir Kh.
If you use a local link as URL, so you should initiate url with fileURLWithPath
method:
如果您使用本地链接作为 URL,那么您应该使用以下fileURLWithPath
方法启动 url :
NSURL *url = [NSURL fileURLWithPath:urlstr];
回答by James Bush
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.
无论 URL 是本地的还是远程的,NSURLConnection 类都应该在涉及 URL 时始终使用。这是加载 AVPlayerItem 或 AVAsset 的唯一线程安全、非侵入式方法。请记住:您的应用程序与其他应用程序共享 iPhone;并且,这是负责任的方式。