Using local player in iOS to play a video from web - ios

I am making a small website for video viewing as a project,
I need to be able to play a video using the local player of the phone, in Android i managed to find the solution:
intent://localhost/video.avi#Intent;action=android.intent.action.VIEW;scheme=http;type=video/mp4;end
This tells the Android OS to open this file using a media player, I need something like this for iOS.
I couldn't find anything on the internet about this, all other posts deal with making an actual application, I don't have access to an iPhone making this even harder to test and play around with it.
How can I do this for iPhone? if not possible are there alternatives?
Notice the code is a mere URL and needs absolutely no further implementations

There's URL Schemes provided by Apple to launch different native stuff via links.
try this: videos://"your video url here"
as an alternative: youtube://"youtube video url here"
About Apple iPhone URL schemes:
https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899-CH1-SW1
Building an iOS application you can use AVFoundation framework, it has AVPlayer (samples are in Swift):
let videoUrl = NSURL(string: "video url here")
let videoPlayer = AVPlayer(URL: videoUrl!)
let playerLayer = AVPlayerLayer(player: videoPlayer)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
you can also use AVPlayerViewController class for it:
let videoUrl = NSURL(string: "video url here")
let videoPlayer = AVPlayer(URL: videoUrl!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
if let player = playerViewController.player {
player.play()
}
}
or you can use a UIWebView to play video from a player within a web page:
let webview = UIWebView(frame: CGRect(x: 0, y: 0, width: 240, height: 375))

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

Creating app that uses files in the files app on iOS

I am attempting to create an iOS application that takes various audio files from the Files app and plays them inside the app using the AudioKit framework. However, from what I have researched, AudioKit's file API appears to be incompatible with external URLs. Does anybody know any alternative music-based frameworks or methods, that can play audio files that are external URL based?
This is the another way. We can play audio with AVPlayer without download. No need to download the audio, with streaming we can play.
Check below code.
let url = URL(string:"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x:0, y:0, width:200, height:200)
self.view.layer.addSublayer(playerLayer)
player.volume = 1.0
player.play()

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")

How to play video from iphone's photo library in my swift app

I have an app where I can record a video using UIImagePicker. On saving the recorded video I want to play the video in UIView within my app.
I am able to record a video and on clicking "use-video" in the UIImagePicker I am printing the video URL (url: /private/var/mobile/Containers/Data/Application/F9DFDFE8-467F-4DDB-A013-C076F3629F06/tmp/51411329851__5CDC14A6-053B-44C1-915D-0E87D9068D6E.MOV).
I want to load the video using the above url and play it in UIView of my app.
Any help will be appreciated.
Thank You.
you can use following code for playing video using AVPlayerViewController
let videoURL = URL(string: YOUR_URL)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}

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

Resources