0

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.

Community
  • 1
  • 1
Tom F
  • 33
  • 1
  • 6
  • http://stackoverflow.com/a/32640305/2303865 for the first part of your question – Leo Dabus Oct 23 '15 at 02:31
  • https://www.dropbox.com/s/kubenqksztck74e/MediaPlayerLink.zip?dl=0 – Leo Dabus Oct 23 '15 at 02:49
  • Thank you Leo for the reply and dropbox link! However, there is one issue I have when I set up your MediaPlayer project together; I am getting an error in ViewController.swift in the "presentViewController" line. It reads "cannot invoke 'presentViewController' with an argument list of type (AnyObject,animated:Bool,completion:nil)'" Would you happen to know what may be the issue? I am seeing what I can do on my end as we speak. – Tom F Oct 23 '15 at 03:09
  • When I try to "rebuild" your project, I am getting a "Failed to co-sign "MediaPlayerLink" and the only way to fix the issue is to be enrolled in a developer program. Very strange stuff. – Tom F Oct 23 '15 at 03:15
  • i will create one without my developer signature – Leo Dabus Oct 23 '15 at 03:25
  • You might need to update your Xcode to the latest version. https://itunes.apple.com/us/app/xcode/id497799835?mt=12 My project was created with Xcode 7.1 released 2 days ago – Leo Dabus Oct 23 '15 at 03:43
  • try also command-shift-K to clean the project before building – Leo Dabus Oct 23 '15 at 03:51
  • Hi Leo, I upgraded Xcode to 7.1 and it now works like a charm! However, something a bit unrelated, before the update I used the "Google Mobile Ads" framework for my previous version of Xcode. Now my project is telling me that there is "no such module 'GoogleMobileAds'". I have updated to the latest version of AdMob and that has not worked, I have also been tampering in the "Search Paths/Framework Search Paths" in the Build Settings. I have been researching for a while and I'm kind of at a loss right now. Would you have any ideas? I feel like it may have to do with the paths, I'm not sure – Tom F Oct 24 '15 at 04:51
  • The comments it is meant for comments not questions. If you have a question feel free to open a new question and if you would like me to take a look just post the link here once posted. – Leo Dabus Oct 24 '15 at 05:55
  • I made a new thread here: http://stackoverflow.com/questions/33337244/upgraded-to-xcode-7-1-google-mobile-ads-framework-not-found – Tom F Oct 26 '15 at 01:43

0 Answers0