Error - AVPlayer is not playing Audio from remote url - Swift - ios

I am trying to play URL audio with AVPlayer. When I put a URL with .mp3 extension, it's working fine. but when I used a URL with no extension. it's not worked. Here's URL
Here's Code.
let url = URL(string: "https://redirector.googlevideo.com/videoplayback?ipbits=0&mn=sn-xcvoxoxu-aixe%2Csn-npoeen7k&ip=117.53.42.8&mm=31%2C29&ms=au%2Crdu&mv=m&mt=1547352278&id=o-AIUfw7X-7-vO4ebqBskvYlVvVmhmgp9WJBItzTwZ7c51&keepalive=yes&pl=24&source=youtube&fvip=6&requiressl=yes&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cexpire&itag=250&gir=yes&clen=4252061&txp=5511222&dur=490.541&lmt=1540081943361780&ei=G7k6XNuzA9TNVv6RjvgI&expire=1547373947&c=WEB&key=yt6&mime=audio%2Fwebm&initcwndbps=143750&signature=774AA79C717B4D9AC6A68743C5730CCBF6A8B9B4.0D91F9D2F80CF12A6623D260B9FC5BB3E69BBAE1&title=Coke-Studio-Season-10-Latthay-Di-Chaadar-Quratulain-Balouch-Farhan-Saeed")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
if player?.rate == 0
{
player.play()
} else {
player.pause()
}
Is there any other way to play audio in iOS with URL's that do not contains .mp3 or any other extension. I don't know why the player is not playing the audio file, there is no sound output on the device and there is no error shown.

Related

Swift AVPlayer Video freeze

guys
I am making a project that plays video using Swift AVPlayer.
There is a problem that the video stops, or there is only sound but no video.
When a video URL is received and played through AVPlyer, the file extension is HLS (.m3u8)
Same issue with both AVPlayer and Safari. However, it plays normally in Chrome.
Is it a video file problem? Or is there something the app needs to handle?
This is the sample video URL I am using
https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8
https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_fmp4/master.m3u8
http://d3rlna7iyyu8wu.cloudfront.net/skip_armstrong/skip_armstrong_stereo_subs.m3u8
self.playerItem = AVPlayerItem(url: URL(string: urlString)!)
self.playerItem?.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status), options: [.initial, .new], context: &self.playerItemContext)
self.playerItemObserver = true
if self.player == nil {
self.player = AVPlayer(playerItem: self.playerItem)
self.playerLayer = AVPlayerLayer(player: self.player)
self.player?.play()
return
}
I am using the above code to insert the url and play it.

Swift AVPlayer continuously loads when trying to play url video file

I am attempting to play a video file from a youtube url. I have created the AVPlayer and linked the url to the player. The player opens when I click the button, but the video player just shows the file continuously loading. I have changed the App Transport Security Settings -> Allow Arbitrary Loads on the plist.
Heres the code:
#IBAction func playVideo(_ sender: Any) {
let movieId = "xLCn88bfW1o"
let path = URL(string: "https://www.youtube.com/watch?v=\(movieId)")
let video = AVPlayer(url: path!)
let videoPlayer = AVPlayerViewController()
videoPlayer.player = video
present(videoPlayer, animated: true, completion: {
video.play()
})
}
EDIT: I've also tried by using the URL for sharing the video; https://youtu.be/xLCn88bfW1o.
The url you are using is not the url to the video, it is the url to a webpage which has the video embedded. There are tools out there that can help you get the actual url of the video file.
For example: YoutubeDirectLinkExtractor
YoutubeDirectLinkExtractor allows you to obtain the direct link to a YouTube video, which you can easily use with AVPlayer. It uses type safety and optionals to guarantee that you won't crash while extracting the link no matter what. There are popular alternatives, which use more straightforward and risky approach, though:
Use extracted video link with AVPlayer:
let y = YoutubeDirectLinkExtractor()
y.extractInfo(for: .urlString("https://www.youtube.com/watch?v=HsQvAnCGxzY"), success: { info in
let player = AVPlayer(url: URL(string: info.highestQualityPlayableLink!)!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}) { error in
print(error)
}
You need to pass the video file's direct URL to AVPlayer (not a Youtube website URL)
For example:
let path = URL(string: "https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4")

AVPlayer & AVAudioPlayer not playing audio from URL

I have trying to play audio from URL. I tried both AVPlayer and AVAudioPlayer but none of them worked.
Here is the URL which I am trying to play.
Here is the code I am using.
// AVPlayer Code
self.avPlayerItem = AVPlayerItem.init(url: urlOfAudio)
self.avPlayer = AVPlayer.init(playerItem: avPlayerItem)
self.avPlayer.volume = 1.0
self.avPlayer.play()
// AVAudioPlayer
if let url = URL.init(string: escapeCharactorString){
do {
self.playerReference = try AVAudioPlayer.init(contentsOf: url)
guard let player = playerReference else {
return
}
player.volume = 1.0
player.prepareToPlay()
player.numberOfLoops = -1
player.play()
} catch let error {
}
}
None of them worked for me.
Please let me know where I am going wrong in it.
There is nothing wrong with your code, it is perfect but the issue is something related to server settings that audio file stored. Here is your working code which successfully playing an other mp3 file,
var avPlayer:AVPlayer?
var avPlayerItem:AVPlayerItem?
override func viewDidLoad() {
super.viewDidLoad()
// let urlstring = "http://www.noiseaddicts.com/samples_1w72b820/2514.mp3"
let urlstring = "https://s3.amazonaws.com/kargopolov/kukushka.mp3"
let url = NSURL(string: urlstring)
print("playing \(String(describing: url))")
avPlayerItem = AVPlayerItem.init(url: url! as URL)
avPlayer = AVPlayer.init(playerItem: avPlayerItem)
avPlayer?.volume = 1.0
avPlayer?.play()
}
So please try the mp3 file with different host it will solve your problem
The cause is Application Transport Security. You need to add:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsForMedia</key>
<true/>
</dict>
to your application's .plist file to load media from HTTP schemes. HTTPS schemes need no such exception.

AVPlayer error with url

I am using the AVPlayer for playing the video. But my problem i.e, the the player is occurring the error. while the same url is already to play in to the Android device and Safari web browser also. If this url replaced by the other url it's working fine.
This is the error.
player.error==========>>>>>>>>>>Optional(Error Domain=AVFoundationErrorDomain Code=-11848 "Cannot Open" UserInfo={NSUnderlyingError=0x156d78f30 {Error Domain=NSOSStatusErrorDomain Code=-12925 "(null)"}, NSLocalizedFailureReason=The media cannot be used on this device., NSLocalizedDescription=Cannot Open})
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let videoUrl = "http://telvuehls_t03007-i.akamaihd.net/hls/live/217085/T03007-calkins/playlist.m3u8"
let playerItem = AVPlayerItem(URL: NSURL(string: videoUrl as String)!)
let playerObj = AVPlayer(playerItem: playerItem)
self.player = playerObj
if playerItem.error == nil{
playerObj.play()
}else{
print("player.error==========>>>>>>>>>>\(playerItem.error)")
}
}
Please refer the following link Supportable Formats for supportable formats of AVPlayer
I think the format you are using to play is not able to open by AVPlayer which is clearly mentioned in the error.
As for i know AVPlayer need chunk of data to play.

How to forward streaming media (music ) in ios?

var url = NSURL(string: musicArr?.objectAtIndex(count) as! String)
player = MPMoviePlayerController(contentURL: url)
self.view.addSubview(player.view)
player.movieMediaTypes.rawValue
player.prepareToPlay()
player.play()
Using movie player , i am playing media from remote URL!
If I click on middle of the slider the mp3 should start from there.

Resources