This question already has answers here:
How to embed a Youtube video into my app?
(7 answers)
Closed 7 years ago.
Okay so I'm looking to play film trailers in my app. The user will press a button, and then it plays the video. I have added the import AVKit and import AVFoundation lines to my file. This is the code I have so far for making the video play:
#IBAction func playTrailerPressed(sender: AnyObject) {
let videoURL = NSURL(string: "https://youtu.be/d88APYIGkjk")
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
This seems to launch an AVPlayerViewController, but doesn't play the video from YouTube. Instead, I get the below:
I have tried both the sharing and embedding link from YouTube, but neither work. If I use a link which has the video file name at the end, it plays it fine, for example: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"- So I know the code works.
Does anyone know if this is possible to use this way with a YouTube video? (I have also tried trailers from IMDb and Apple, but it's the same).
Thanks for your help!
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.
Related
New to Swift! I am trying to have the app play a video from my Resources upon pressing a button. I have this method in my ViewController:
if let path = Bundle.main.path(forResource: "Cypress", ofType: "mov") {
let player = AVPlayer(url: URL(fileURLWithPath: path))
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
}
However, when the video begins to play, it is a black screen but the audio is playing properly. Are there any quick fixes to this? I am using Xcode version 12.2. Any advice would be greatly appreciated thanks :)
The problem is with the video file itself / the encoding.
Deceptively, the Quicktime ".mov" format is not a video codec, but rather audio/video container format that can contain video and audio (and some other things) in any number of compression codecs (h.264, mpeg2, ProRes, MJPEG, AAC, mp3, etc.) … Your files don't work because they include video compressed with a codec which iOS does not support.
From #alexkent in this SO answer on another post.
I don't think a .mov is necessarily not going to work for you, but you'd have to make sure it was encoded in a way that is iOS-compatible. So as you mentioned in the comments, using .mp4 ensures the video is encoded in an iOS-compatible fashion.
I'm confused on how an AVAsset gets and plays video from a remote url. I have looked at the documentation but am still a little bewildered by AVAssets, items, and players.
The main questions that I have are
Does AVKit automatically manage downloading while playing or does the entire video need to be downloaded in order to play from a remote url?
If I played back the asset in a looping fashion, would it download the video again and again?
let video = AVAsset(url: "https://www.w3schools.com/html/mov_bbb.mp4")
let item = AVPlayerItem(asset: video)
player.replaceCurrentItem(with: item)
player.automaticallyWaitsToMinimizeStalling = false
player.play()
There is so much stuff in Stackoverflow about playing videos in cell, that every person may feel confused, especially that most of question are not solved and it looks like that every developer gives up.
So I wanted to create new question where there will be up-to-date answers.
Objective:
Play video in UITableViewCell or UICollectionViewCell when user taps cell (yes, this is simplified version, this is not about autplay).
Solution:
I think everyone agrees that we should use AVPlayer instead of deprecated MPMoviePlayerController.
Cell should have layer where AVPlayer can be assigned, so in my code while creating cell I added this code:
VideoCell.swift
let layer = AVPlayerLayer()
layer.backgroundColor = UIColor.clear.cgColor
layer.videoGravity = AVLayerVideoGravityResizeAspectFill
Now when cell is tapped I'm using this code:
ViewController.swift
cell.playerLayer.player = player
player.replaceCurrentItem(with: AVPlayerItem(asset: asset))
player.play()
Where player is global instance of AVPlayer and asset is AVAsset initialized with video URL.
It would work fine, but AVPlayer needs time to download info and buffer some part of video, and what user gets is delay. So we can think about one solution, which is downloading video first and playing it locally when users taps in cell.
Question:
Is it really good approach to downloading every video in data source and then playing it locally? When should we remove it from device? Facebook and Instagram looks like they download only part of the video first, is there any who can share own experience?
Objective-C code will be appreciated too.
I am trying to play a remote mp4 file which is there on the web using AVPlayer on TVOS. When i try to play the video i can hear the audio but there is no video and whole screen is blank. I have read many articles about that which say that i should turn off ATS (I did that. However my url is HTTPS) and i set the frame also in viewDidLayoutSubviews, but still unable to make it work. Here is my code, if some could help me in solving this issue then it will be a great help since i am scratching my head here and there since yesterday.
I am not writing the exact Url for some privacy issues.
import Foundation
import UIKit
import AVKit
class PlayViewController : UIViewController {
var moviePlayer : AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
print("In PlayViewController View Did Load")
let movieUrl = NSURL.init(string: "Some Amazon Url.mp4")!
moviePlayer = AVPlayer.init(URL: movieUrl)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let avPlayerLayer = AVPlayerLayer.init(layer: moviePlayer!)
avPlayerLayer.frame = CGRectMake(0.0, 0.0, 1024.0, 768.0)
self.view.layer.addSublayer(avPlayerLayer)
moviePlayer?.play()
}
}
The URL is somehow indicating HTTP(S) streaming, right?
Well, I also made my experience while trying to play a video stream from a set-top-box like a DVB receiver through its Web-Interface. It did not work (at least when I tried with tvOS 9.0). According to Apple documentation the video streams have to comply to the HTTP Streaming protocol they describe. It requires, that HTTP streams are split into small chunks of video snippets of a few seconds length. See https://developer.apple.com/library/tvos/referencelibrary/GettingStarted/AboutHTTPLiveStreaming/about/about.html
At the end I gave up and switched to use VLCKit from videolan.org. After some back and forth this works fine in my App (vuplusTV).
I am trying to use AVFoundation, AVPlayer and AVPlayerViewController to show a video. Does anyone know of any good tutorials out there that does NOT use MPMoviePlayer?
Please let m eknow!
thank you!
AVPlayerViewController
AVPlayerViewController is the AVKit ViewController that hides away most of the complexity involved with dealing directly with AVFoundation. Its very simple and intuitive, if you check the AVPlayerViewController Class Reference you will notice that it does not hold that many properties, if you want to initialise one and attribute a video to it, you have three options ( that I can recall ).
Initialise it in code and set the frame of it;
Subclass it and call it when you wanna use it;
Use Storyboards / nibs to allocate a AVKit Player View Controller.
Either way its up to you and this is a common pattern among iOS developers, so it shouldn't strike as a surprise to you unless you are a complete beginner. Please note that for any of these ways of creating an AVPlayerViewController, you have to initialise the AVPlayer object with an AVPlayerItem or an URL carrying the video you want to display.
Subclassing AVPlayerViewController-Swift
Obs. This is the swift example, but the Objective-C one follows the same pattern, the only change is the syntax of each language.
class AVKitPlayerViewController: AVPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
// get the video URL
let videoURL = NSBundle.mainBundle().URLForResource("Test", withExtension: ".mov")
// Initialise with URL
player = AVPlayer(URL: videoURL!)
// Initialise with AVPlayerItem
let playerItem = AVPlayerItem(URL: videoURL!)
player = AVPlayer(playerItem: playerItem)
// If you want custom controls showsPlaybackControls should be false
// Then you must create your own buttons
//showsPlaybackControls = false
}
}
AVFoundation
AVFoundation is a very large and interesting framework, you can do a lot of advanced and interesting stuff with it. I would encourage you to go read the docs and checkout these WWDC-2015 sample code:
AVFoundationPiPPlayer: Picture-in-Picture Playback with AVKit
AVFoundationQueuePlayer-iOS: Using a Mixture of Local File Based Assets and HTTP Live Streaming Assets with AVFoundation
AVFoundationSimplePlayer-iOS: Using AVFoundation to Play Media
AVFoundation Programming Guide
AVFoundation Programming Guide
I hope this helps you. Happy Coding. :smile: