How to play youtube video using MPMoviePlayerController? - ios

I want to play video of youtube url.
I'm using below code but it is not working.
-(void)playVideoFromURL
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%s","https://www.youtube.com/embed/96ReVjMAXEE?autoplay=1&vq=small"]];
self.videoController = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.videoController setControlStyle:MPMovieControlStyleNone];
self.videoController.repeatMode=MPMovieRepeatModeOne;
self.videoController.fullscreen=YES;
self.videoController.scalingMode=MPMovieScalingModeFill;
self.videoController.view.frame=CGRectMake(0,0,self.videoplayview.frame.size.width, self.videoplayview.frame.size.height);
[self.videoplayview addSubview:self.videoController.view];
[self.videoController play];
}

You cannot play a YouTube vidoe URL in MPMoviePlayerController. For this you have to use
youtube-iso-player-helper - But you cannot play private video URL in youtube-iso-player-helper
XCDYoutubeKit - It is against YouTube Terms and Service.

Unfortunately, there's no way to directly play a youtube video with MPMoviePlayerController because youtube does not expose direct links to the video files.
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.

Related

AVPlayer video preview issue iOS 14.0.1

I am trying to play local and remote videos using AVPlayer and AVPlayerViewController but I am facing an issue. The player is playing the audio for video but not showing the preview.
It is working fine on all the iOS versions less than iOS 14.0.1.
Please help me on this issue
Here is the sample code
movieUrl = [NSURL fileURLWithPath:filePath];
AVPlayer *player = [AVPlayer playerWithURL:movieUrl];
theMoviPlayer = [[AVPlayerViewController alloc] init];
theMoviPlayer.player = player;
[self addSubview:theMoviPlayer.view];
theMoviPlayer.view.frame = self.bounds;
[theMoviPlayer.player play];
Look at this image for better understanding
I have found the actual reason of this issue. It seems that for certain non-standard encoding or fps, AVPlayer is not able to play video. I have exported the same video in correct encoding using some tool i.e, QuickTimePayer, and the video is playable now.
This issue is only producing on iOS 14.0.1, may be it is a bug/feature in AVPlayer in this update.
Conclusion:
Always check your video encoding if your video is not playing or you are just hearing only audio without any video frames.

MKV player in my iOS app

How can I play mkv video into my iOS App?
Can I use MPMoviePlayerController and add to it mkv codecs?
I try to load an mkv from an URL into my MPMoviePlayerController
NSURL *videoStreamURL = [NSURL URLWithString:#"mkvURL"];
mediaPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoStreamURL];
But a black screen, with nothing in it to play, appears.
The simple answer is no.
You can use FFMPEG to accomplish this using https://github.com/iMoreApps/ffmpeg-avplayer-for-ios

Youtube Playlist in iphone app

I'm wondering wether its possible to play a youtube playlist in an IOS App. i've been struggling to find an answer for this for some time now. i've tried several html5 players, but they do not support youtube playlists. Other than that i've tried using this simple code:
NSURL *url = [NSURL URLWithString:array[selectedRowValue]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
But this will only play the playlist and you wont be able to navigate between the playlist videos.
my question is then is there an API or any kind of sample codes easy to use for this purpose?
i'm thinking about something like this:
You could try implementing a UIWebView into the tableview's header view and have the url change whenever a user selects a specific cell.
You can find the youtube player webview sample in the answer here YouTube Embed player in Iframe doesn't work in iOS6

iOS - Video Streaming, Prevent Video from being downloaded or saved locally

Is there any way to play Video Streaming via URL in iOS native App which can not be downloaded or saved locally. I want to let the user watch video but he should not be able to download or save video locally (via 3rd party downloader apps).
Is above case can be possible in MPMoviePlayerController, MPMoviePlayerViewController or WebViewController?
Any suggestion will be appreciated.
It is possible via MPMoviePlayerViewController.
NSURL *url = [NSURL URLWithString:yourLink];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];

Play YouTube videos with MPMoviePlayerController instead of UIWebView

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

Resources