Alex Tau
Alex Tau

Reputation: 2639

AVPlayer Can't Play

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.

Upvotes: 3

Views: 10300

Answers (3)

James Bush
James Bush

Reputation: 1525

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.

Upvotes: 0

Amir Kh.
Amir Kh.

Reputation: 444

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

NSURL *url = [NSURL fileURLWithPath:urlstr];

Upvotes: 1

Bishnu Pada Chanda
Bishnu Pada Chanda

Reputation: 5416

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

Upvotes: 9

Related Questions