Unable to play .mp4 video file using AVPlayer - ios

I am using the AVPlayer to play the video from url.
func playVideoFromUrl(urlString: String){
let videoURL = URL(string: urlString)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
let delegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate
delegate.window?.rootViewController?.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
The url of video:- "http://gsn-input-dev.s3.amazonaws.com/public/video/00229/9229d4ad2a0c547e7bfa51e3fbef806f.mp4"
I am not getting any warning at console. Below is the screenshot of simulator. What I am doing wrong?

I have accomplished what you've wanted to do. Here is my approach:
First import AVFoundation, AVKit(I believe you had) then I have configured my viewcontroller like this
#IBOutlet weak var avPlayerView: UIView! // drag a UIView to view controller
var videoPlayer = AVPlayer()
var avPlayerViewController = AVPlayerViewController()
override func viewDidLoad() {
super.viewDidLoad()
let videoURL = URL(string: "http://gsn-input-dev.s3.amazonaws.com/public/video/00229/9229d4ad2a0c547e7bfa51e3fbef806f.mp4")
self.playVideo(url: videoURL!)
}
playVideo function is configured like below
func playVideo(url: URL) {
self.videoPlayer = AVPlayer(url: url)
self.avPlayerViewController = AVPlayerViewController()
self.avPlayerViewController.player = self.videoPlayer
avPlayerViewController.view.frame = avPlayerView.frame
self.addChildViewController(avPlayerViewController)
self.view.addSubview((avPlayerViewController.view)!)
}
Open your info.plist as Source Code
then add this to info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
screen shot is given below

Have you tried adding arbitrary load flag in your info.plist file as i see the url is a HTTP url and to make the HTTP url work, we need to specify it in app transportation security via info.plist as follows
Please try doing this as video is working perfectly fine with you code.
Read following document for more info on App Transportation Security
I hope this will help you :)

I recently found out that a device running iOS 14 using AVPlayer cannot play .mp4 files. A compatible video format that works on AVPlayer is .mov.

I also have had a trouble playing .mp4 files. When I made the URL end with .mp4 it works.

Related

Vimeo video for iOS Application HCVimeoVideoExtractor is not working [duplicate]

I create an iOS app that make use of Vimeo for playing videos. I was wondering what the best method is to show Vimeo videos within iOS Swift.
I have fixed that, when an image is clicked, the placeholder image will be hidden, but the video won't play directly. Beside that all Vimeo Interface elements are visible. I know you should have to give credits to Vimeo, so it goes without saying that I display an Vimeo logo at the bottom of the video. Is there a possibility to hide all Vimeo elements of the web player?
Sources where I can find more information would be nice.
If there are any questions left, let me know!
Thanks in advance.
You can use this Swift library HCVimeoVideoExtractor to extract the mp4 video URL then use AVPlayer to play it. Simply pass the Vimeo video link or video id.
let url = URL(string: "https://vimeo.com/254597739")!
HCVimeoVideoExtractor.fetchVideoURLFrom(url: url, completion: { ( video:HCVimeoVideo?, error:Error?) -> Void in
if let err = error {
print("Error = \(err.localizedDescription)")
return
}
guard let vid = video else {
print("Invalid video object")
return
}
print("Title = \(vid.title), url = \(vid.videoURL), thumbnail = \(vid.thumbnailURL)")
if let videoURL = vid.videoURL[.Quality540p] {
let player = AVPlayer(url: videoURL)
let playerController = AVPlayerViewController()
playerController.player = player
self.present(playerController, animated: true) {
player.play()
}
}
})
The best way that I've been able to figure out is to use a WKWebView or UIWebView. Steps:
Add a WKWebView of desired size inside the View Controller.
Get the video embed code from the original Vimeo video (Click on the "Share" button below the video to find the embed code.)
Click on "More Options" on the Vimeo Share sheet that pops-up to configure the look of the embed video.
Use the following code sample to embed the video:
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(webView)
let embedHTML="<html><head><style type=\"text/css\">body {background-color: transparent;color: black;}</style><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes\"/></head><body style=\"margin:0\"><div><iframe src=\"//player.vimeo.com/video/139785390?autoplay=1&title=1&byline=1&portrait=0\" width=\"640\" height=\"360\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div></body></html>"
let url = URL(string: "https://")!
webView.loadHTMLString(embedHTML as String, baseURL:url )
webView.contentMode = UIViewContentMode.scaleAspectFit
The way that I've managed to implement this is using AVKit and AVFoundation:
let url: URL! = URL(string: "https://01-lvl3-pdl.vimeocdn.com/01/3355/3/91775232/243724947.mp4?expires=1498547278&token=073f7b03877a8ed3c8029")
let player: AVPlayer = AVPlayer(url: url)
let controller: AVPlayerViewController = AVPlayerViewController()
controller.view.translatesAutoresizingMaskIntoConstraints = false
controller.player = player
// Add `controller` to your view somehow
player.play()
The trick being that you cannot use the link where it displays the website (in my case https://vimeo.com/91775232).
I had to inspect the source and find the actual URL for the video (i.e.: https://01-lvl3-pdl.vimeocdn.com/01/3355/3/91775232/243724947.mp4?expires=1498547278&token=073f7b03877a8ed3c8029).
Once I used that, everything worked fine.
For Swift 4, I have used WKWebView in storyboard and implemented following code:
import WebKit
#IBOutlet weak var webKitView: WKWebView!
To play video in WebView :
if yourVimeoLink.lowercased().contains("vimeo.com") {
let url: NSURL = NSURL(string: yourVimeoLink)
webKitView.contentMode = UIViewContentMode.scaleAspectFit
webKitView.load(URLRequest(url: url as URL))
}
Hope will help! :)
import AVFoundation
import AVKit
let videoURL = "https://vimeo.com/blablabla/whatevertheURLis"
func playExternalVideo() {
let videoURL = NSURL(string: self.videoURL)
let player = AVPlayer(url: videoURL as! URL)
let playerViewController = AVPlayerView()
playerViewController.player = player
}
Make sure to add the AVKit Player View and give it a class of AVPlayerView. As for the Vimeo deal, it should play without Vimeo's UI Controls. For more help watch this video. https://www.youtube.com/watch?v=fhD7hXrpExE

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.

How to play Vimeo videos in iOS Swift?

I create an iOS app that make use of Vimeo for playing videos. I was wondering what the best method is to show Vimeo videos within iOS Swift.
I have fixed that, when an image is clicked, the placeholder image will be hidden, but the video won't play directly. Beside that all Vimeo Interface elements are visible. I know you should have to give credits to Vimeo, so it goes without saying that I display an Vimeo logo at the bottom of the video. Is there a possibility to hide all Vimeo elements of the web player?
Sources where I can find more information would be nice.
If there are any questions left, let me know!
Thanks in advance.
You can use this Swift library HCVimeoVideoExtractor to extract the mp4 video URL then use AVPlayer to play it. Simply pass the Vimeo video link or video id.
let url = URL(string: "https://vimeo.com/254597739")!
HCVimeoVideoExtractor.fetchVideoURLFrom(url: url, completion: { ( video:HCVimeoVideo?, error:Error?) -> Void in
if let err = error {
print("Error = \(err.localizedDescription)")
return
}
guard let vid = video else {
print("Invalid video object")
return
}
print("Title = \(vid.title), url = \(vid.videoURL), thumbnail = \(vid.thumbnailURL)")
if let videoURL = vid.videoURL[.Quality540p] {
let player = AVPlayer(url: videoURL)
let playerController = AVPlayerViewController()
playerController.player = player
self.present(playerController, animated: true) {
player.play()
}
}
})
The best way that I've been able to figure out is to use a WKWebView or UIWebView. Steps:
Add a WKWebView of desired size inside the View Controller.
Get the video embed code from the original Vimeo video (Click on the "Share" button below the video to find the embed code.)
Click on "More Options" on the Vimeo Share sheet that pops-up to configure the look of the embed video.
Use the following code sample to embed the video:
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(webView)
let embedHTML="<html><head><style type=\"text/css\">body {background-color: transparent;color: black;}</style><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes\"/></head><body style=\"margin:0\"><div><iframe src=\"//player.vimeo.com/video/139785390?autoplay=1&title=1&byline=1&portrait=0\" width=\"640\" height=\"360\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div></body></html>"
let url = URL(string: "https://")!
webView.loadHTMLString(embedHTML as String, baseURL:url )
webView.contentMode = UIViewContentMode.scaleAspectFit
The way that I've managed to implement this is using AVKit and AVFoundation:
let url: URL! = URL(string: "https://01-lvl3-pdl.vimeocdn.com/01/3355/3/91775232/243724947.mp4?expires=1498547278&token=073f7b03877a8ed3c8029")
let player: AVPlayer = AVPlayer(url: url)
let controller: AVPlayerViewController = AVPlayerViewController()
controller.view.translatesAutoresizingMaskIntoConstraints = false
controller.player = player
// Add `controller` to your view somehow
player.play()
The trick being that you cannot use the link where it displays the website (in my case https://vimeo.com/91775232).
I had to inspect the source and find the actual URL for the video (i.e.: https://01-lvl3-pdl.vimeocdn.com/01/3355/3/91775232/243724947.mp4?expires=1498547278&token=073f7b03877a8ed3c8029).
Once I used that, everything worked fine.
For Swift 4, I have used WKWebView in storyboard and implemented following code:
import WebKit
#IBOutlet weak var webKitView: WKWebView!
To play video in WebView :
if yourVimeoLink.lowercased().contains("vimeo.com") {
let url: NSURL = NSURL(string: yourVimeoLink)
webKitView.contentMode = UIViewContentMode.scaleAspectFit
webKitView.load(URLRequest(url: url as URL))
}
Hope will help! :)
import AVFoundation
import AVKit
let videoURL = "https://vimeo.com/blablabla/whatevertheURLis"
func playExternalVideo() {
let videoURL = NSURL(string: self.videoURL)
let player = AVPlayer(url: videoURL as! URL)
let playerViewController = AVPlayerView()
playerViewController.player = player
}
Make sure to add the AVKit Player View and give it a class of AVPlayerView. As for the Vimeo deal, it should play without Vimeo's UI Controls. For more help watch this video. https://www.youtube.com/watch?v=fhD7hXrpExE

streaming videos from google drive in swift 2

im trying to stream videos from google drive but its not working
this is my code
func playVideo () {
let url = NSURL(string: "https://drive.google.com/file/d/0B9XuPgWwSkzrdHN5WWUydjJScDg/preview")!
let player = AVPlayer(URL: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player?.play()
}
}
i get this when i start the function image here
i solved the problem by Decodeing the html from the link and take the steam link from it
you can see the code here : https://github.com/DevZaid/GDSLink
your link is to flash video, not to some video supported by AVPlayer.

Resources