Play YouTube videos with MPMoviePlayerController instead of UIWebView - ios

I'm trying to stream some youTube videos using the MPMoviePlayerController but I'm having some problems. The code I'm using is pretty simple and I can play .m4v videos by passing a URL to initWithContentURL. When I launch the movie player the player comes up but just goes away after about 20 seconds. When I try it in the simulator I get an alert view that says the server is not configured correctly. Is there an argument I need to pass with the URL to get a specific type of video feed from google?
NSURL *videoURL = [NSURL URLWithString:#"http://www.youtube.com/v/HGd9qAfpZio&hl=en_US&fs=1&"];
MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer play];
I've also tried the following URL's
http://www.youtube.com/watch?v=HGd9qAfpZio
I have also seen the argument &format=1 and tried to add that to the end of both of the strings but no luck.

The only way to have a youtube video play inside your own app is to create a UIWebView with the embed tag from Youtube for the movie you want to play as the UIWebView's content. UIWebView will detect that the embedded object is a Youtube link, and the web view's content will be the youtube preview for the video. When your user clicks the preview, the video will be shown in an MPMoviePlayerController. This is the technique described at the link that Muxecoid provided (how to play youtube videos within an application), and this is (as far as I know of) the only way to have a youtube video play within an application. You can still have the Youtube application launch by passing the youtube URL to -[UIApplication openURL:], but of course this closes your own application which is often undesirable.
Unfortunately, there's no way to directly play a youtube video with MPMoviePlayerController because youtube does not expose direct links to the video files.

If you are using
Code:
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
Source:
Just refer this link
Make sure that you test it on device and not on the simulator. Since simulator will always display question mark with blue box (it doesn't have quick-time player).

As I wrote above in my comment I had to do this in a project I was working on. I've solved this problem and submitted the code on github.
You can check the source and fork it here.
It basically takes a youtube url and gets all the iOS compatible video urls.
Hope this is to any help!
Cheers

I just stumbled across someone who was able to place a YouTube video inside an MPMoviePlayerController. It does appear possible now.
LBYouTubeView

To play you tube videos you need to extract the url before passing the url into MPMoviePlayer. You can't play it directly.
My github repo has a demo implementation of this.
See:
https://github.com/DpzAtMicRO/IOSYoutubePlayer
Try it, this makes it possible to load and play youtube video directly in MPMoviePlayerlike any other video and is pretty good approach too.
EDIT: Make sure that you go through the Readme.md well before using this in your project

It's as simple as taking the src element of the iframe for the new embed code and calling
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];

Check the following link. I think it will help you.
https://mobiletechfeast.blogspot.in/2014/08/youtube-player-for-ios.html

There is an official way to embed YouTube video in iOS application which is provided by Google.
Install library via cocoapods: pod "youtube-ios-player-helper"
import by: #import “YTPlayerView.h”
Use UIView and set class to YTPlayerView or create object of YTPlayerView programatically.
Load video: [self.playerView loadWithVideoId:#"M7lc1UVf-VE"];
There is also playVideo and stopVideo methods available. For more info visit this Link

Related

How to play Youtube Video from a browser app on non full screen in Objective C

I am working on Browser app, I want when user searches any video on youtube.com the video is playing on full screen but I want the video to be open on a fixed size small frame, Say Half of phone screen size.
I have Video url and video id. I have integrated YOUTube API but as soon as I hit play button on youtube player it plays on full screen.
Please find codebase of integration
self.playerView = [[YTPlayerView alloc]init];
self.playerView.frame = webView.frame;
[self.playerView loadWithVideoId:str];
[webView.superview addSubview:self.playerView];
Please find the screenshots
When youtube player is added and After tap on play button
Help will be much appreciated.
Does you project have Controller named MainViewController , that controller should have issue with YTPlayerView, please check for this view in your view controller.
in most of the cases it will not defined in the class you have directly use in the code
Searching for the issue in your code i found a property of UIWebview which will help you in playing video inline setAllowsInlineMediaPlayback
With help of this you can directly play the youtube video in the webview,
Here is code sample which you can use to play youtube video
NSString *strVideoId = #"M7lc1UVf-VE";
NSString *videoURL = [NSString stringWithFormat:#"https://www.youtube.com/embed/%#?feature=player_detailpage&playsinline=1",strVideoId];
NSString* embedHTML = [NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe width=\"100%%\" height=\"240px\" src=\"%#\" frameborder=\"0\"></iframe>\
</body></html>",videoURL];
[self.mainwebview setAllowsInlineMediaPlayback:true];
[self.view addSubview:self.mainwebview];
[self.mainwebview loadHTMLString:embedHTML baseURL:nil];
In this code you can pass any video id which you were passing in the loadWithVideoId of YTPlayerView
To change the width/height you can make change in the width=\"100%%\" height=\"240px\" portion of the iframe
It is working for me in the iPhone also which is looking like this:
iframe youtube play in iPhone
Another reference the setAllowsInlineMediaPlayback working in iPhone: https://stackoverflow.com/a/15189889/4557505
The image which you shown in the screenshot looks like particular video issue
Note This code does not require YTPlayerView so if this code works for you, you can remove the YTPlayerView from your project if this is not used anywhere else
Update Some video not playing:
I think this issue has not nothing to do with the specific implementation as for me it's not working in browser(with embed Url which we are setting in iFrame) or YTPlayerview also
But till than i check and may be find some issue in implementation and as you have mentioned:
Yes all videos worked on YTPlayerView. These videos work on normal
full screen mode but not with embedded player
If you want to make implementation in the YTPlayerview you can check the following code:
NSString *strVideoId = #"3bMYgo_S0Kc";
self.playerView = [[YTPlayerView alloc]init];
self.playerView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height/2);
[self.playerView loadWithVideoId:strVideoId playerVars:#{#"playsinline":#1}];
[_mainwebview addSubview:self.playerView];
[self.mainwebview setAllowsInlineMediaPlayback:true];
[self.view addSubview:self.mainwebview];
In the loadWithVideoId we can provide the inline parameter which can solve the problem of full screen and this may work for you on the iPhone also
Note this video is not working on YTPlayer or in browser(embedded url) for me but based on your comment you can try this
#Pyro
setAllowsInlineMediaPlayback works only for ipad, I have already enabled it. and it does now worked, It is not allowing to play the video.
Image after implementation

UIWebView to play Html 5 video without going to Video Player of iOS

I want to use video player technique used in my website. I use html 5 videos in website and send normal URL to app so that they can be played using the native MPMoviePlayerController inside the app. Now I have requirements where I want to use the same technique as web inside the app as UIWebView. When I request the link to load, it opens up the default player view of iOS, which I want to avoid.
Can this be possible?
No, this is not possible. Not in the Safari browser. Videos will always be played by the native player. Trust me, I've spent a lot of time on this.
If you build a Phonegap app, it is possible by what is mentioned here:
HTML5 inline video on iPhone vs iPad/Browser
Basically:
HTML
<video id="player" width="480" height="320" webkit-playsinline>
Obj-C
webview.allowsInlineMediaPlayback = YES;

How to play YouTube video in iOS not in fullscreen?

I've been trying to get YouTube music videos to play in my iOS app in a small window (not full screen) and can't get it to work. I've tried the following:
I first used the YouTube API and created a YTPlayerView as instructed here: https://developers.google.com/youtube/v3/guides/ios_youtube_helper#adding_ytplayerview. This allowed me to play videos in-line, but many videos were unable to be played because of licensing issues, stating "This video contains content from *. It is restricted from playback on certain sites."
I then tried using a UIWebView, which successfully played the music videos that were previously unavailable. However, these videos were always forced to play in fullscreen mode, even when I set webView.allowsInlineMediaPlayback = YES;
I also tried embedding the video using the technique discussed here: How to embed YouTube video on iOs and play it directly on UIWebview without full screen. This worked to keep videos inline, but again, rendered certain videos unplayable.
I finally tried using an MPMoviePlayerController, but later found out that it can't actually play YouTube videos, as we don't have access to the direct links of video files, so this was another no-go.
Apps like PingTune were clearly able to figure this out using YouTube - they are able to play YouTube music videos in a custom, non-fullscreen view and without ads - what am I missing? Is there a part of the YouTube API that allows me to play these music videos that I implemented incorrectly?
I've never tried it myself but you could try LBYouTubeView by Laurin Brandner. 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.
Code sample
LBYouTubePlayerViewController* controller = [[LBYouTubePlayerViewController alloc] initWithYouTubeURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=1fTIhC1WSew&list=FLEYfH4kbq85W_CiOTuSjf8w&feature=mh_lolz"] quality:LBYouTubeVideoQualityLarge];
controller.delegate = self;
controller.view.frame = CGRectMake(0.0f, 0.0f, 200.0f, 200.0f);
controller.view.center = self.view.center;
[self.view addSubview:self.controller.view];

radio streaming without MPMoviePlayerController

i am currently doing a small radio shoutcast for android and ios with kony.
everything is fine with android but for ios, i am creating a library to play the shoutcast.
Unfortunatly, it seems that the player of MPMoviePlayerController need to be set as a subview to work and i am unable to do that.
does anybody know an other solution to play a shoutcast on IOS?
I don't know about shoutcast but I imagine its similar to SoundCloud in which you use the API to parse the stream URL? If I'm right in thinking that then you simply need to put the URL into an NSURL and make sure to tell the MPMoviePlayerController the source, like so...
radioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:YOUR_SHOUTCAST_STREAM_URL]];
radioPlayer.movieSourceType = MPMovieSourceTypeStreaming;
Then simply play
[radioPlayer prepareToPlay];
Hope that helps :)

How to play youtube video without UIWebView or detect video player when youtube video start playing using webview?

I need to play youtube video in my iOS application and while video is playing I need to add an overlay on video.
1.Now how can i run youtube video in native player?
2.if I play video in UIWebview then how can i detect that video is
playing and how to add overlay on the video?
MPMoviePlayerViewController cannot directly play youtube video urls.You need to extract the url before loading MPMoviePlayerViewController with the url.
you can see a working demo in my github repo:
Try this::
https://github.com/DpzAtMicRO/IOSYoutubePlayer
LBYoutubeExtractor extracts the youtube Url with JSON.
Please do read the README.md before you use this in your application.
Regards
MPMoviePlayerViewController is a good one but last commit is years ago. I found another solution called 'XCDYouTubeKit' which supports iPhone/iPad and iOS7 onwards with full screen YouTube video.
You can access github page at here and fully documented tutorial at here
This is what official developer says in his page:
The only official way of playing a YouTube video inside an app is with a web view and the iframe player API. Unfortunately, this is very slow and quite ugly, so I wrote this player to give users a better viewing experience.

Resources