I am trying to play a YouTube video from a URL in my iOS app using MPMoviePlayerController but I just get a black screen and nothing plays. What am I doing wrong? I have verified that the url is correct and is being passed the correct videoId.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlString = [NSString stringWithFormat:#"https://www.youtube.com/watch?v=%#", videoIDString];
NSURL *videoURL = [NSURL URLWithString:urlString];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
moviePlayerController.shouldAutoplay = YES;
moviePlayerController.fullscreen = YES;
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
}
Related
I want to play video on clicking a button. My AVPlayerViewController is open but not playing an video .Please help.
- (IBAction)btnPlay_Click:(id)sender {
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
If you are using vimeo video you can use third party tools which helps you load the vimeo video.
Solution 1: Please try this, It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}
Solution 2: YTVimeoExtractor
May be it will help you.
Use MPMoviePlayerController and add this line of code:
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
MPMoviePlayerViewController *player =[[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
[self.view addSubview: [player view]];
[self presentMoviePlayerViewControllerAnimated:player];
[player.moviePlayer prepareToPlay];
Add one more line : [self.view addSubview: [player view]];
movieController is an instance of MPMoviePlayerViewController declared in the .h file.
[movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.
--------- If this is not working you can just do that following ----------
player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player.view setFrame:CGRectMake(0, 0, 320, 320)];
[player play];
[self.view addSubview:player.view];
I want to play video in my UIView from the URL that i got from the JSON Response from server.
This is code that i wrote to play video but nothing happens.
NSURL *movieURL = [NSURL URLWithString:objAlbum.strVideoURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayerController.view setFrame:self.movieView.bounds];
[self.movieView addSubview:moviePlayerController.view];
// Configure the movie player controller
moviePlayerController.controlStyle = MPMovieControlStyleNone;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
In strVideoURL i'm getting video url.
Check that URL has any spaces. Better to Add below line of code
NSURL *movieURL = [NSURL URLWithString:[objAlbum.strVideoURL stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayerController.view setFrame:self.movieView.bounds];
// Configure the movie player controller
moviePlayerController.controlStyle = MPMovieControlStyleNone;
//[moviePlayerController prepareToPlay];
[moviePlayerController play];
[self.movieView addSubview:moviePlayerController.view];
Note: Print the movie URL and play using browser to check it is valid URL or not..!
Hope it helps you...!
i have solve this using following code
NSURL *vidURL = [[NSURL alloc]initWithString:objAlbum.strVideoURL];
self.movie = [[MPMoviePlayerController alloc]initWithContentURL:vidURL];
self.movie.controlStyle = MPMovieControlStyleDefault;
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
but i want to open it now in UIView defined in TableViewCell like video open in FB(Facebook)
When i tried a video saved on the Xcode project , it worked.
then when i changed it with a url , it always returns this error :
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
Here's my code :
NSURL *url = [NSURL fileURLWithPath:#"https://m.youtube.com/watch?v=9MNAeGWd_04"];
movieController = [[MoviePlayerVC alloc]initWithContentURL:url];
movieController.view.frame = self.view.bounds;
[movieController.moviePlayer setShouldAutoplay:YES];
[movieController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[[movieController view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[self presentViewController:movieController animated:NO completion:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateDidChange:)
name:#"MPAVControllerPlaybackStateChangedNotification"
object:nil];
fileURLWithPath: expects a local path url - /User/blah/blah.mp3
Try to use [NSURL URLWithString:#"https://m.youtube.com/watch?v=9MNAeGWd_04"]
IF YOU PLAY FROM URL THEN TRY THIS
-(void)playFromLiveUrl
{
NSURL *aUrl = [NSURL URLWithString:#"*YOUR URL*"];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:aUrl];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}
IF YOU ARE USE LOCAL FILE THEN USE THIS
-(void)playFromLocal
{
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:#"** LOCAL PATH ** "]];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}
The MPMoviewPlayerController Can't get your format. U r try to load the MPMoviePlayerController with youTube link, it won't work (as u can see :)
Here u can find the iPhone video format support.
Try to load your MoviePlayer something like this (for example .m3u8 format):
[movieController setContentURL:[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"]];
MPMoviePlayerController cannot play a Flash Youtube URL. Use a web view instead. UIWebView on iOS is smart enough play youtube content.
You can see the video formats MPMoviePlayer and AVFoundation support here
Making an app for my church and need mp3 file to play after being downloaded from xml. NSLog displays the url correctly, but when I run it, I just get a spinning "loading" and a black box. Any suggestions as to why?
Thanks!
-(void)viewDidAppear:(BOOL)animated
{
RSSItem* item = (RSSItem*)self.detailItem;
self.title = item.title;
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:item.link];
NSLog(#"The url is %#", item.link);
moviePlayerController.view.frame = CGRectMake(73, 203, 640, 360);
[self.view addSubview:moviePlayerController.view];
[moviePlayerController prepareToPlay];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
TRY this one..
RSSItem* item = (RSSItem*)self.detailItem;
self.title = item.title;
if(item.link)
{
//NSURL *url = [NSURL fileURLWithPath:item.link];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:item.link];
moviePlayer.view.frame = CGRectMake(0, 0, 500, 500);
moviePlayer.moviePlayer.shouldAutoplay=YES;
moviePlayer.moviePlayer.controlStyle = MPMediaTypeMusicVideo;
[moviePlayer.moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer play];
}
else
{
NSLog(#"no url");
}
I was wondering how to play multiple videos in one page, like where you have two buttons, and when you press one, it plays a video, and when you press the other it plays a different one. I have this code so far:
-(void)WelcomeVideo1
{
NSURL *url31 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"DirectorsWelcome" ofType:#"mp4"]];
welcomePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url31];
welcomePlayer1.controlStyle = MPMovieControlStyleDefault;
welcomePlayer1.shouldAutoplay = YES;
[self.view addSubview:welcomePlayer1.view];
[welcomePlayer1 setFullscreen:YES animated:YES];
}
-(void) moviePlayBackDidFinish:(NSNotification *)aNotification{
[welcomePlayer1.view removeFromSuperview];
welcomePlayer1 = nil;
}
- (void)moviePlayerWillExitFullscreen:(NSNotification*) aNotification {
[welcomePlayer1 stop];
[welcomePlayer1.view removeFromSuperview];
welcomePlayer1 = nil;
}
-(void)storyVideo1
{
NSURL *url4 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"OurStory" ofType:#"mp4"]];
storyPlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url4];
storyPlayer1.controlStyle = MPMovieControlStyleDefault;
storyPlayer1.shouldAutoplay = YES;
[self.view addSubview:storyPlayer1.view];
[storyPlayer1 setFullscreen:YES animated:YES];
}
-(void) moviePlayBackDidFinish2:(NSNotification *)aNotification{
[storyPlayer1.view removeFromSuperview];
storyPlayer1 = nil;
}
- (void)moviePlayerWillExitFullscreen2:(NSNotification*) aNotification {
[storyPlayer1 stop];
[storyPlayer1.view removeFromSuperview];
storyPlayer1 = nil;
}
but whenever I try to play both videos, the 2nd one I play crashes the app. Any ideas?
Use AVPlayer and AVPlayerLayer instead of MPMoviePlayerController.
Take a look into following apple example.