AVPlayer Can't Play

前提是你 提交于 2019-12-07 15:57:29

问题


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.


回答1:


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!!!




回答2:


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

NSURL *url = [NSURL fileURLWithPath:urlstr];



回答3:


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.



来源:https://stackoverflow.com/questions/6818254/avplayer-cant-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!