Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

NSURL *url = [NSURL fileURLWithPath:urlstr];

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top