I'm have some video files ready for streaming on my server. Here's the code I'm trying to use to play them in an iOS app:
Video *vid = [videos objectAtIndex:index];
NSURL *vidUrl = [NSURL URLWithString:vid.videoUrl];
NSLog(#"%#",vid.videoUrl);
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:vidUrl];
player.controlStyle=MPMovieControlStyleEmbedded;
[player.view setFrame:self.view.bounds];
[self.view addSubview:player.view];
[player play];
If I copy and paste the URL the NSLog spits out into Safari, the video plays fine. So I know the URL is good. But in my app, I just get a black screen. What's wrong with my code?
MPMoviePlayerController needs to be a strong property declared as so #property (strong, nonatomic) MPMoviePlayerController *mvpc;
Then whenever you want to play a movie you would write:
self.mvpc = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"movieURL"]];
self.mvpc.shouldAutoplay = YES; //Optional
self.mvpc.controlStyle = MPMovieControlStyleEmbedded;
[self.mvpc prepareToPlay];
[self.mvpc.view setFrame:self.view.bounds];
[self.view addSubview:self.mvpc.view];
[self.mvpc play];
Related
I am currently trying to retrieve an .mp4 from a URL.
The video is accessible from a web-browser but the play button is crossed out. Any ideas on what could be going wrong?
Code in question
NSURL *movieURL = [NSURL URLWithString:self.videoURL];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:movieURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;
Screenshot of device
I think there is no way apart from UIWebView to play youtube video inside iOS application. Also you can redirect to youtube application by passing your video url.So youtube application will handle rest of the things
You can read the guidelines of YouTube section 2 point 10 here
You can use youtube player here
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"IMG_0597" ofType:#"mov"]];
AVURLAsset *asset = [AVURLAsset assetWithURL:movieURL];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
[playerViewController.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)];
playerViewController.delegate=self;
playerViewController.showsPlaybackControls = YES;
[self.view addSubview:playerViewController.view];
[player play];
The reason why my videos did not play was because in the xml, where I was retrieving the url for the videos, I did not start the url with http://.
The code for this player works as it should.
I have made one sample demo
Play video on my simulator using AVPlayer.
But my problem is video is not showing where audio of that video is working.
Code is:
#property (nonatomic,strong)AVPlayerViewController *videoPlayer;
-(IBAction)playVideo:(id)sender {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"video" ofType:#"mp4"]];
AVPlayer *player = [AVPlayer playerWithURL:url];
self.videoPlayer = [[AVPlayerViewController alloc]init];
[self presentViewController:self.videoPlayer animated:YES completion:nil];
self.videoPlayer.player = player;
self.videoPlayer.showsPlaybackControls=YES;
self.videoPlayer.view.frame = self.view.frame;
[self.view addSubview:self.videoPlayer.view];
[player play];
}
Image
I guess that the problem is in your video file. Try to download another video mp4 video sample. Add it to your project and try.
You don't need this code:
self.videoPlayer.view.frame = self.view.frame;
[self.view addSubview:self.videoPlayer.view];
I am very new to IOS app development , I am trying to play a video from gallery through MPMoviePlayer. I have list of all the video's urls in an array, but when trying to play the video MPMoviePlayer launches but does not play any video. Here is the code that I am using:
NSURL *fileURL = [NSURL fileURLWithPath:[urlArr objectAtIndex:indexPath.row]];
playMovie = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:playMovie.view];
playMovie.fullscreen = YES;
[playMovie play];
Below is the code from apple documentation, I think you are not using [player prepareToPlay], which Prepares the player for playback by preloading its buffers. rest all looks fine.
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play]
A video from the internet like so:
NSURL *url = [NSURL URLWithString:#"http://www.example.com/myvideo.m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];
Unable to play movie on Simulator Black Screen displays
You should try some frameworks (for example HCYoutubeParser) that can fetch a direct link to video from Youtube for you.
// Gets an dictionary with each available youtube url
NSDictionary *videos = [HCYoutubeParser h264videosWithYoutubeURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=8To-6VIJZRE"]];
// Presents a MoviePlayerController with the youtube quality medium
MPMoviePlayerViewController *mp = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[videos objectForKey:#"medium"]]] autorelease];
i want to play video in ios one after another..I have two video files .m3u8 format. if video one completed then video two start automatically.. after video two complete then again video one should played automatically. and again video two should start automatically. how can i do this thing….I am using Mediaplayer framework. plz give me any idea…..Thanks in advance.
-(void)viewDidLoad
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"buyTutorial" ofType:#“m3u8”];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
[self setController:moviePlayerController];
}
You should be able to use this notification:
https://developer.apple.com/LIBRARY/IOS/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/c/data/MPMoviePlayerPlaybackDidFinishNotification
And then look for this constant PlaybackEnded:
https://developer.apple.com/LIBRARY/IOS/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html#//apple_ref/c/tdef/MPMovieFinishReason
Then just start the next movie in your playback queue.
Here's some examples on how to register to the notification:
MPMoviePlayerPlaybackDidFinishNotification being called again in iPhone 4.3 simulator when setting contentURL