Play a video from Youtube in a AVPlayerViewController in Swift
AVPlayer
only plays movie files, and YouTube videos aren't directly exposed as movie files at their URL. It looks like the preferred way to handle YouTube videos is to embed a web view into your app. See this page for information from Google on how to do that.
How to play video with AVPlayerViewController (AVKit) in Swift
Swift 3.x - 5.x
Necessary: import AVKit, import AVFoundation
AVFoundation framework is needed even if you use AVPlayer
If you want to use AVPlayerViewController:
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
or just AVPlayer:
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
It's better to put this code into the method: override func viewDidAppear(_ animated: Bool) or somewhere after.
Objective-C
AVPlayerViewController:
NSURL *videoURL = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:^{
[playerViewController.player play];
}];
or just AVPlayer:
NSURL *videoURL = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
[player play];
How to embed a Youtube video into my app?
Xcode 8.2 • Swift 3.0.2
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var wv: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
loadYoutube(videoID: "oCm_lnoVf08")
}
func loadYoutube(videoID:String) {
guard
let youtubeURL = URL(string: "https://www.youtube.com/embed/\(videoID)")
else { return }
wv.loadRequest( URLRequest(url: youtubeURL) )
}
}
Xcode 7.3.1 • Swift 2.x
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")
}
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) )
}
}
Related Topics
Admob Sdk - iOS -File Not Found
Font Size on Universal Storyboard
Ios7 App Backward Compatible with iOS5 Regarding Unique Identifier
Uipinchgesturerecognizer. Make Zoom in Location of Fingers, Not Only Center
Choosing Units with Measurementformatter
Uicontrolstate.Normal Is Unavailable
What Does It Mean for Something to Be Thread Safe in iOS
Why Does "Try Catch" in Objective-C Cause Memory Leak
Why Isn't Preferredcontentsize Used by iPhone 6 Plus Landscape
iOS How to Dismiss Uialertview with One Tap Anywhere
Perform Action by Clicking on Some Word in Uitextview or Uilabel
How to Query Nearest Users in Firebase with Swift
What's Wrong Here: Instance Member Cannot Be Used on Type
Uipickerview as Inputview of Uitextfield
Cncopycurrentnetworkinfo with iOS 13
Apple Push Notification Service Server Certificate Update
How to Get the Modulus or Exponent from a Seckeyref Object in Swift