How to open youtube video in app? - ios

I would like to play youtube video in my app. But I want to open a video to full screen by image(button) clicking. I don't want to create UIWebview. Is it possible?

Use this pod: XCDYouTubeKit.
func StartVideo() {
let ViodeoViewController = AVPlayerViewController()
self.present(ViodeoViewController, animated: true, completion: nil)
XCDYouTubeClient.default().getVideoWithIdentifier("KHIJmehK5OA") { (video: XCDYouTubeVideo?, error: Error?) in
if let streamURL = video?.streamURLs[XCDYouTubeVideoQuality.HD720.rawValue] {
ViodeoViewController.player = AVPlayer(url: streamURL)
} else {
self.dismiss(animated: true, completion: nil)
}
}
}
you can also change other configurations setting with the help of library.

Please follow below links:
This is helper provided by youtube, you can install this pod and can use second link to understand how to integrate in your app
https://github.com/youtube/youtube-ios-player-helper
https://developers.google.com/youtube/v3/guides/ios_youtube_helper

Related

Swift YouTube video in Full Screen & Auto Play with SFSafariViewController

Problem:
I would like SFSafariViewController to autoplay a YouTube video in full screen.
Code:
import SafariServices
#objc func handleYTTap(_ sender: UIGestureRecognizer) {
let youtubeVideoURL = "https://youtu.be/d9MyW72ELq0"
let bookmark = NSURL(string: youtubeVideoURL)
let safari = SFSafariViewController(url: bookmark as! URL)
if(UIDevice.current.userInterfaceIdiom == .pad) {
safari.modalPresentationStyle = .fullScreen
}else{
safari.modalPresentationStyle = .fullScreen
}
self.present(safari, animated: true, completion: nil)
}
Tried Solutions:
I have tried to implement
"?playsinline=1?autoplay=1".
to the url but still cant get the SFSafariViewController to autoplay in full screen. is this even possible to do without creating a custom view or using a library of some sort?

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

Open Camera Programmatically Through iOS App - Deep Link?

I want to create an IBAction to open the iOS native camera app in my app, but I can't seem to find the address for the camera app online.
I know for messages it's: UIApplication.shared.open(URL(string: "sms:")!, options: [:], completionHandler: nil)
Does anyone know which is the correct scheme?
I suggest you to follow a clean way doing so:
let cameraVc = UIImagePickerController()
cameraVc.sourceType = UIImagePickerControllerSourceType.camera
self.present(cameraVc, animated: true, completion: nil)
in such case you must add into the Info.plist:
<key>NSCameraUsageDescription</key>
<string>whatever</string>
Here we suggest you to user the following github :
https://github.com/shantaramk/AttachmentHandler
!. 1. Drag drop the AttachmentHandler folder in project folder
func showCameraActionSheet() {
AttachmentHandler.shared.showAttachmentActionSheet(viewController: self)
AttachmentHandler.shared.imagePickedBlock = { (image) in
let chooseImage = image.resizeImage(targetSize: CGSize(width: 500, height: 600))
self.imageList.insert(chooseImage, at: self.imageList.count-1)
self.collectionView.reloadData()
}
}
Swift 5 version, after adding NSCameraUsageDescription in your Info.plist:
let cameraVc = UIImagePickerController()
cameraVc.sourceType = UIImagePickerController.SourceType.camera
self.present(cameraVc, animated: true, completion: nil)

Xcode Swift play youtube video on Native video player?

i am looking for pod on swift to play Youtube video on ios native player just like XCDYouTubeKit unfortunately XCDYouTubeKit is only for objective-C i can't find any swift pod work like this , all i found they embed iframe video and the youtube logo will appear which will allow user to click on it and miss up the page when the full page open .
how i can play youtube video on ios native video player ? without html embed !
Exactly like :
Although XCDYouTubeKit is written in Objective-C, it integrates fine with Swift. Here is some sample code to use it with Swift 3:
struct YouTubeVideoQuality {
static let hd720 = NSNumber(value: XCDYouTubeVideoQuality.HD720.rawValue)
static let medium360 = NSNumber(value: XCDYouTubeVideoQuality.medium360.rawValue)
static let small240 = NSNumber(value: XCDYouTubeVideoQuality.small240.rawValue)
}
func playVideo(videoIdentifier: String?) {
let playerViewController = AVPlayerViewController()
self.present(playerViewController, animated: true, completion: nil)
XCDYouTubeClient.default().getVideoWithIdentifier(videoIdentifier) { [weak playerViewController] (video: XCDYouTubeVideo?, error: Error?) in
if let streamURLs = video?.streamURLs, let streamURL = (streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?? streamURLs[YouTubeVideoQuality.hd720] ?? streamURLs[YouTubeVideoQuality.medium360] ?? streamURLs[YouTubeVideoQuality.small240]) {
playerViewController?.player = AVPlayer(url: streamURL)
} else {
self.dismiss(animated: true, completion: nil)
}
}
}

How Do I Present a Music Library With a View Controller for a MPMediaPickerControllerDelegate using Swift 3 iOS10?

I am trying to follow a recent post on using a MPMediaPickerControllerDelegate to present a music selection list.
The tutorial is found at this URL:
http://www.justindoan.com/tutorials/
I am using this code:
import UIKit
import MediaPlayer
class ViewController: UIViewController, MPMediaPickerControllerDelegate {
var mediapicker1: MPMediaPickerController!
override func viewDidLoad() {
super.viewDidLoad()
let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.music)
mediaPicker.allowsPickingMultipleItems = false
mediapicker1 = mediaPicker
mediaPicker.delegate = self
self.presentViewController(mediapicker1, animated: true, completion: nil)
}
}
However I have found that the:
self.presentViewController(mediapicker1, animated: true, completion: nil)
does not work. Unfortunately, Swift 3's suggested automatic solution does not work either:
self.present(mediapicker1, animated: true, completion: nil)
Furthermore, the iOS 10 Beta Release Notes, found at:
https://www.scribd.com/doc/315770725/IOS-10-Beta-Release-Notes
says on page 10 of 18,
An MPMediaPickerController object may not display as expected.
I have spent a great deal of time looking to solve this issue on my own with no success.
Any suggestions?
Go through the steps:
Add 'NSAppleMusicUsageDescription' to your Info.plist for the privacy authority.
Make sure your Music app is available in your iPhone. It will not work in the simulator.
As per our discussion over comments, I recently use the MPMediaPickerController in my latest app named playmates. I am sharing the working code with you. I have written the self-explanatory code.
import MediaPlayer
class viewControllerName: UIViewController,MPMediaPickerControllerDelegate {
//Below is Inaction for picking music from media library
#IBAction func btnMediaPickerAction(_ sender: UIButton) {
let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.music)
mediaPicker.delegate = self
mediaPicker.allowsPickingMultipleItems = false
self.present(mediaPicker, animated: true, completion: nil)
}
// MPMediaPickerController Delegate methods
func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
self.dismiss(animated: true, completion: nil)
}
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
self.dismiss(animated: true, completion: nil)
print("you picked: \(mediaItemCollection)")//This is the picked media item.
// If you allow picking multiple media, then mediaItemCollection.items will return array of picked media items(MPMediaItem)
}
}
I found below discripencies in your code:
use self.present not self.presentViewController
No need to create global instance of mediaPicker. As in the delegate method you are getting the instance of MPMediaPickerController
Add Privacy - Media Library Usage Description in info.plist

Resources