How to play video player one after another in ios? - ios

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

Related

Video is not showing me use AVPlayer only audio is working

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];

video not playing through MPMoviePlayer

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]

How to add video to iOS app intro screen like Uber

Looking to add a video to my apps intro screen like the Uber app. I am sure there are others.
I found this which uses a gif and UIWebView, but not sure if this is the best solution. I definitely don't want to use images and stitch them together (would rather not have video if that is the preferred method).
https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2
This is what I use in my app Letsplay, The answer from Aslam will work but uses MPmoviePlayerController which is depreciated as of IOS 9.0.
Also I set the video gravity so that your video will fill the entire frame which means no black borders.
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"icebergs" ofType:#"mp4"];
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath: videoPath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:[AVURLAsset URLAssetWithURL:videoURL options:nil]];
AVPlayer* videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer* videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:videoPlayer];
[videoPlayerLayer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view.layer addSublayer:videoPlayerLayer];
[videoPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[videoPlayer play];
If you don't want sound then remember to mute the player
[videoPlayer setMuted:YES];
I haven't tried the code, but I it should work. It generally takes the path of the video file and adds it to the view controller. You may have to set the coordinates and size. Though I have used here video, GIF is more preferred.
- (void)viewDidAppear:(BOOL)animated
{
[self loadVideoInBackgroundOfView];
}
-(void)loadVideoInBackgroundOfView{
MPMoviePlayerController* videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getTheVideoPath]];
[videoPlayer.view setFrame:self.view.bounds];
[self.view insertSubview:moviePlayer.view atIndex:0];
[videoPlayer prepareToPlay];
[videoPlayer play];
}
-(NSURL*)getTheVideoPath{
NSString *path = [[NSBundle mainBundle] pathForResource:#"VideoFileName" ofType:#"mp4"];
NSURL *URL = [NSURL fileURLWithPath:path];
return URL;
}

AVPlayer not playing local videos

I've been struggling with this for some hours...
I'm trying to play a video I download and save using AVPlayer but nothing shows up.
What surprised me the most is that if I use Apple's sample url for testing http live streaming
that video does play. (http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8)
However, when I change the url to the local file path the video doesn't play. I recorded the video with the iPhone and verified that the format (.mov) is playable. Here is the code block:
AVPlayer *mPlayer = [[AVPlayer alloc] init];
//mPlayer = [mPlayer initWithURL:[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
mPlayer = [mPlayer initWithURL:filePath];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:mPlayer];
[[[self view] layer] insertSublayer:playerLayer atIndex:3];
[playerLayer setFrame:self.view.bounds];
[mPlayer play];
NSLog(#"Playing video at: %#", filePath);
Sample output:
Playing video at: /var/mobile/Applications/B298EE7D-D95D-4CCC-8466-2C0270E2071E/Documents/394665262.174180.mov
EDIT:
Solved by using [NSURL fileURLWithPath:local_url_string]
Just small piece of code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"splash" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
AVPlayer* player = [AVPlayer playerWithURL:videoURL];

MPMoviePlayerController do not load movie

I want to play video in my iPad app, using MPMoviePlayerController. My problem is, that player is only displaying "Loading" and do not play the video. Here is the code I use:
- (void)playMovieFile:(NSString *)fileName
{
NSString *filePath=[[NSBundle mainBundle] pathForResource:fileName ofType:#"mov"];
NSURL *fileUrl=[NSURL fileURLWithPath:filePath];
self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:fileUrl];
[self.moviePlayer.view setFrame:self.view.frame];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
}
I also try to use MPMoviePlayerViewController, but with sane success.Also, what movie types do MPMoviePlayerController plays? Can it be because of size of movie? It is 263 MB.
The movie formats supported on the iPhone are .mov, .m4v, mp4 or 3gp.
The video technologies in iOS support the playback of movie files with the .mov, .mp4, .m4v, and .3gp filename extensions and using the following compression standards:
In Case you playing the movie with above mentioned extensions then. declare your MPMoivePlayer as class type property in your .h class
#property(strong,nonatomic)MPMoviePlayerController *moviePlayer;
in your .m
NSString *filePath=[[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"mov"];
NSURL *fileUrl=[NSURL fileURLWithPath:filePath];
self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:fileUrl];
[self.moviePlayer.view setFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];

Resources