I am trying to create an iOS app written in Swift that can play a video from the internet. It can be a video from a streaming link or from a YouTube URL, whatever you think is easier. I personally prefer a streaming link for my work, if that is possible.
My goal is to be able to play a video from the internet that a user can scrub through, as well as pause and continue playing if the user chooses to. Basic video player controls.
In android, I can successfully stream from archive.org using this as an example streaming video: https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4
However, in the iOS side I've been having some issues.
Here is my attempt to play that streaming video:
In this attempt, I get a black screen with a crossed-out play arrow
import UIKit
import AVKit
import AVFoundation
import MediaPlayer
class MediaVideo1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var url:NSURL = NSURL(string: "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4")!
let player = AVPlayer(URL: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is my attempt to play a YouTube video:
In this attempt, I use a WebView in my ViewController. When I run the app, I get a video thumbnail, but when I click it, I get the error message "An error has occurred, please try again later" which pops up all of the time for this code.
import UIKit
import MediaPlayer
class MediaVideo1: UIViewController {
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
var html = "<html><body><iframe src=\"http://www.youtube.com/embed/aGDE519ygkw\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
webView.loadHTMLString(html, baseURL: nil)
webView.allowsInlineMediaPlayback = true
let embededHTML = "<html><body><iframe src=\"http://www.youtube.com/embed/aGDE519ygkw?playsinline=1\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
webView.loadHTMLString(html, baseURL: NSBundle.mainBundle().bundleURL)
}
}
I'm not incredibly pleased with this direction mostly because the WebView is finnicky when it comes to placing it in the ViewController. I have done a lot of work with measurements and keeping the aspect ratio but it never looks "correct".
I have seen a YouTube library that I can download on GitHub, but it's not written in swift and seems like a huge headache.
These issues kind of push me towards the "streaming idea" instead.
EDIT: I am being informed that my post is a possible duplicate of another thread. However, I am still seeing errors.
How to embed a Youtube video into my app?
I am seeing errors on the "guard" "else { return }" and "wv.loadrequest" lines
import UIKit
class ViewController: UIViewController {
// create an outlet for your webview
@IBOutlet weak var wv: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// load your you tube video ID
loadYoutube(videoID: "oCm_lnoVf08")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func loadYoutube(videoID videoID:String) {
// create a custom youtubeURL with the video ID
guard
let youtubeURL = NSURL(string: "https://www.youtube.com/embed/\(videoID)")
else { return }
// load your web request
wv.loadRequest( NSURLRequest(URL: youtubeURL) )
}
}
Someone has also posted a dropbox file within the thread using the exact code and I have encountered the same issues.
Again, I feel like I should be leaning more towards the streaming side than the YouTube side most likely.