Play Youtube Videos with Swift - swift

Hey I want to display a youtube video in my app but if I run it there is only a black screen ??
func playVideo() {
var audioplayer : MPMoviePlayerController!
var url:NSURL = NSURL(string: "https://youtu.be/7n1KPclvGQY.mp4")!
var MPMoviePlayerViewController = MPMoviePlayerController(contentURL: url)
MPMoviePlayerViewController.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)
MPMoviePlayerViewController.movieSourceType = MPMovieSourceType.File
self.view.addSubview(MPMoviePlayerViewController.view)
MPMoviePlayerViewController.prepareToPlay()
MPMoviePlayerViewController.play()
MPMoviePlayerViewController.pause()
}

There's no really nice way to do this. There is this library but it breaks YouTube's TOS.
Best to call [UIApplication sharedApplication] openURL:...] which will open in safari or the YouTube app, if they have it installed.
If its content you own, perhaps try hosting it yourself and using the same method yourself?

Use custom library for playing youtube video
Make use of custom https://github.com/larcus94/LBYouTubeView
It is a subclass of MPMoviePlayerViewController.
LBYouTubeView is just a small view that is able to display YouTube videos in a MPMoviePlayerController. You even have the choice between high-quality and standard quality stream.
It just loads the HTML code of YouTube's mobile website and looks for the data in the script tag.
LBYouTubeView doesn't use UIWebView which makes it faster and look cleaner.

Related

Rich notification play video clip

I'm trying to get rich notification and play video on the notification.
I success to show an image and not found swift sample code.
What need to be done to add video clip / mp4 notification support?
which function need to add to the NotificationService class ?
Thanks Yakir
There is no need to add extra code and no need to use AV player also. we can achieve that by initialising UNNotificationAttachment with url which we get after URLSession.shared.downloadTask
URLSession.shared.downloadTask(with: attachmentURL) { url, _ , _ in
let attachment = try UNNotificationAttachment(identifier: "identifier", url: url, options: nil)
content.attachments.append(attachment)
}
While updating the content in NotificationServiceExtension. Notification will load the video if it is video url.
Note: - please make sure video should be small size or as suggested by Apple.

UIWebView/WKWebView and youtube streaming videos

I'm trying to play streaming videos in my app but it's not working in WKWebView/UIWebView.
let url = URL(string: "https://www.youtube.com/embed/PP2G3dm_fsc")
let webView = WKWebView()
webView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: webViewHeight)
webView.load(URLRequest(url: url!))
webView.isUserInteractionEnabled = true
self.addSubview(webView)
This video (PP2G3dm_fsc) is restricted, but I've got the same error message with any video (even with my own). At the same time this url works in my browser like a charm.
Do I have to use YTPlayerView to make it work?
update: I have the same results with YTPlayerView
You can use youtube-ios-player-helper.
It is an open source library that helps you embed a YouTube iframe player into an iOS application. The library creates a UIWebView and a bridge between your application’s Objective-C code and the YouTube player’s JavaScript code, thereby allowing the iOS application to control the YouTube player.
This will make your implementation easier with the use of the library. You can also check their GitHub page for sample implementation.
Hope this helps.

AVPlayer doesn't play video [duplicate]

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.

Load YouTube outside Application

I have a simple UIWebView showing a page including YouTube thumbnail. When I click it, it loads it inside the application, but I want it to be loaded outside application in the YouTube-app.
How can this be done?
Thanks.
It should be as simple as loading the YouTube-video-url like this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=abcde12345"]];
It won't work in the simulator because it doesn't have the YouTube-app, but it will work on the iPhone (i hope, untested).
(I also see that your accept-rate is pretty low, you should click the little ✔ next to the answer that you think is the best answer. That way people wondering about the same thing can find the most useful answer fast and the person posting the answer will be accredited.)
EDIT:
Here is an example of what you could do to catch the YouTube-urls and opening them in the YouTube-app instead of in your app:
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Determine if we want the system to handle it.
NSURL *url = request.URL;
if ([url.host isEqual:#"youtube.com"] && ([url.query rangeOfString:#"watch"] != 0)) {
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
return NO;
}
}
return YES;
}
This is not tested, but should work :)
From http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html
Method 2: Embed the YouTube player in a UIWebView
No, the iPhone still doesn't do Flash, but if you haven't already
noticed, the Safari browser on the iPhone is clever enough to turn any
YouTube embed into a clickable thumbnail that launches the native
YouTube player app on the phone. You can take advantage of this
feature in your app by using a UIWebView. Here's how:
Set up a UIWebView in your app. You can make it part of a xib or create it programmatically. Size the UIWebView according to how large
you want the clickable thumbnail to be.
Grab the video url using the same method as the one described above.
Call the loadHTMLString:baseURL: method on the UIWebView instance with some carefully constructed HTML that contains the YouTube
embedded player code snippet and some supporting HTML to make sure
that the video thumbnail appears correctly. Set the base URL to the
URL of your website (it doesn't do anything here -- ordinarily
UIWebView uses it to handle relative URL links correctly).
The best way to illustrate this is with a code snippet. Note the use
of the viewport HTML meta parameter and the consistent use of width
and height parameters throughout.
// webView is a UIWebView, either initialized programmatically or
loaded as part of a xib.
NSString *htmlString = #" ";
[webView loadHTMLString:htmlString baseURL:[NSURL
URLWithString:#"http://www.your-url.com"]];
One of the biggest benefits of this approach is that your app does not
have to quit in order for the video to start playing. In fact, the
iPhone will keep your app running in the background while it fires up
the YouTube player to play the video. After the video finishes playing
(or when the user hits "Done"), the user is automatically taken back
to your app. This experience is very similar to watching embedded
YouTube videos in the iPhone Safari browser and is just as seamless.

Is it possible to play vimeo video in iphone native application using UIWebView approach?

There is UIWebView approach to play YouTube video inside native IPhone Application.
Does anybody tried to do the sane for vimeo?
It's only possible if Vimeo supports HTML5 video, which looks like it does.
NSString *htmlString = [NSString stringWithFormat:
#"<html>"
#"<body>"
#"<meta name = \"viewport\"content = \"initial-scale = 1.0, user-scalable = no\"/>"
#"<iframe src=\"http://player.vimeo.com/video/8118831title=0&byline=0&portrait=0&color=008efe&amp\";autoplay=1&loop=1 width=\"320\" height=\"480\" frameborder=\"0\">"
#"</iframe>"
#"<body style=\"background:#000;margin-top:0px;margin-left:0px\">"
#"</object></div></body></html>",#"http://www.vimeo.com/8118831"
];
Now just use loadHTMLString to play the video in your application.
It is only possible when user has shared his video using "plus" account. Otherwise vimeo will render static image in Iphone.