AVPlayer cannot play

I want to play remote mp3 from AVPlayer. I cannot make it work, and I see no reason why it does not work. The 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 upload it to webView, it will start playback immediately. Please, help.

+3
source share
3 answers

I have 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 this works for you.

+9
source

If you use a local link as a URL, you must initiate the URL using the fileURLWithPath method:

 NSURL *url = [NSURL fileURLWithPath:urlstr]; 
+1
source

The NSURLConnection class, whether the URL is local or remote, should always be used whenever the URL is used. It is the only thread-safe, non-intrusive AVPlayerItem or AVAsset download tool. Remember: your application supports iPhone with other applications; and, this is the way to be responsible insodoing.

0
source

Source: https://habr.com/ru/post/921645/


All Articles