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]
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 would like to loop a mp4 file in UIWebView.
e.g.
If you go to this link (http://techslides.com/demos/sample-videos/small.mp4) to open UIWebView, you won't be able to play it in loop. But I would like to watch this mp4 video for loop and loop again.
Please give me any tips to overcome!
Cheers,
You can simply use MPMoviePlayerController with setRepeatMode:MPMovieRepeatModeOne.
This should allow seamless looping of any video, and it's simple to implement.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Your Cool Video File" ofType:#"mp4"]];
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer prepareToPlay];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
[playerController.moviePlayer play];
Important to remember to subscribe to the end playback notification like so:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
Start playback manually in moviePlayBackDidFinished method
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
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];