I need to quit the MPMoviePlayerController programmatically while playing, instead of pressing done button. Is it possible. Is there any way to simulate Done button click?
i have one trick for you. you can take the mpmovieplayer on uiview and then remove the uiview after stoping the player
In ViewController.h
MPMoviePlayerController *moviePlayerController;
UIView *view1;
-(IBAction)cancelPlay:(id)sender;
In ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"try" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
view1=[[UIView alloc]initWithFrame:CGRectMake(0, 10, 320, 300)];
view1.backgroundColor=[UIColor blueColor];
[self.view addSubview:view1];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 10, 320,300)];
[view1 addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
moviePlayerController.controlStyle=MPMovieControlStyleEmbedded;
[moviePlayerController play];
}
-(IBAction)cancelPlay:(id)sender{
[moviePlayerController stop];
[view1 removeFromSuperview];
}
Related
i want to play video of 4 second in app after default black image is launched what can i do? please help me
my code is below
-(void)viewWillAppear:(BOOL)animated
{
NSString* moviePath = [[NSBundle mainBundle] pathForResource:#"intro_anim" ofType:#"mov"];
NSURL* movieURL = [NSURL fileURLWithPath:moviePath];
NSLog(#"%#",movieURL );
playerCtrl = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[playerCtrl prepareToPlay];
playerCtrl.scalingMode = MPMovieScalingModeFill;
playerCtrl.controlStyle = MPMovieControlStyleNone;
playerCtrl.view.frame = CGRectMake(0, 0, 480, 320);
[playerCtrl.view setCenter:CGPointMake(240, 160)];
[playerCtrl.view setFrame:CGRectMake(0, 0, 480, 320)];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[playerCtrl play];
[self presentMoviePlayerViewControllerAnimated:playerCtrl.view];
[self.view addSubview:playerCtrl.view];
}
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
{
NSString *url;
UIButton *btnvideo;
UIButton *btnaudio;
MPMoviePlayerController *player;
MPMoviePlayerController *Player;
NSArray *arr;
}
#property(nonatomic,retain)IBOutlet UIButton *btnvideo;
#property(nonatomic,retain)IBOutlet UIButton *btnaudio;
-(void) moviefinished:(NSNotification *)anotification;
-(void) Audiofinished:(NSNotification *)bnotification;
-(IBAction)PlayAudio:(id)sender;
-(IBAction)PlayVideo:(id)sender;
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
-(IBAction)PlayVideo:(id)sender
{
url=[[NSBundle mainBundle]pathForResource:#"nfc" ofType:#"mp4"];
player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:url]];
player.view.frame=CGRectMake(0, 0, 320, 300);
[self.view addSubview:player.view];
[player play];
}
-(IBAction)PlayAudio:(id)sender
{
[player release];
url=[[NSBundle mainBundle]pathForResource:#"bgm" ofType:#"mp3"];
Player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:url]];
Player.view.frame=CGRectMake(0, 0, 320, 300);
[self.view addSubview:Player.view];
[Player play];
}
-(void) moviefinished:(NSNotification *)anotification
{
player=[anotification object];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player autorelease];
}
-(void) Audiofinished:(NSNotification *)bnotification
{
player=[bnotification object];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:Player];
[Player autorelease];
}
-(void)presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController *)moviePlayerViewController
{
[player setControlStyle:MPMovieScalingModeAspectFit];
}
Note- please mind it to connect playaudio button action to
Insted of a default image use a view controller as splash and push the "first viewcontroller" using timer. You can do whatever you want on your "splash view controller."
How can I resize the MPMoviePlayerController dynamically to fix all the size of screens for iPhone/iPad (both portrait & landscape)?
this is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *about = [[UIBarButtonItem alloc] initWithTitle:#"About" style:UIBarButtonItemStyleDone target:self action:#selector(openPopup)];
self.navigationItem.leftBarButtonItem = about;
NSURL *streamURL = [NSURL URLWithString:#"http:/streamUrl/playlist.m3u8"];
_streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
self.streamPlayer.controlStyle = MPMovieControlStyleEmbedded;
[self.view addSubview: self.streamPlayer.view];
[self.streamPlayer play];
}
I also tried to add
[self.streamPlayer.view setFrame:self.view.bounds];
but this makes the video is not fix to screen as shown in screenshot
How can I make it fix all screens of all different devices?
Thanks in advance.
The best solution I found is to use the following
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
[self.streamPlayer.view setFrame:CGRectMake(10, 10 ,height-20, width-70)];
i solved this problem in ios 5
as:
AvPlayerLayer *avPlayerLayer =[[AVPlayerLayer alloc]init];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];
avPlayerLayer.frame = self.playerView.layer.bounds;
avPlayerLayer.transform = CATransform3DMakeRotation(M_PI / 2.0, 0, 0, 1);
[self.playerView.layer addSublayer:avPlayerLayer];
here is avPlayerLayer.transform you can set it in any direction .
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:contentURL]];
if (moviePlayerViewController) {
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
[moviePlayerViewController.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
if ([moviePlayerViewController.moviePlayer respondsToSelector:#selector(setAllowsAirPlay:)]) {
[moviePlayerViewController.moviePlayer setAllowsAirPlay:YES];
}
[[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController queue:nil usingBlock:^(NSNotification *notification) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self dismissMoviePlayerViewControllerAnimated];
}];
[moviePlayerViewController.moviePlayer play];
}
While a movie is playing, if I change my contentURL the movie stops playing and the view is removed.
I've tried the solution here but it still doesn't work.
Here's my code to play the movie:
- (void) playMovie:(NSURL *)moviePath{
theMovie = [[MPMoviePlayerController alloc] initWithContentURL: moviePath];
UIView * movieView = [theMovie view];
[movieView setFrame: CGRectMake(0, 0, 480, 320)];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[movieView setTransform: landscapeTransform];
self.theMovie.scalingMode = MPMovieScalingModeAspectFit;
self.theMovie.fullscreen = TRUE;
self.theMovie.controlStyle = MPMovieControlStyleNone;
[self performSelector:#selector(showCtrlsOnTouch) withObject:NULL afterDelay:0.1];
[self.theMovie prepareToPlay];
int a = currentMovie;
int b = [self.tableDataSource count] - 1;
if(a < b)
{
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
nextButton.frame = CGRectMake(360.0, 138.0, 100.0, 44.0);
[nextButton setTitle:#"Next" forState:UIControlStateNormal];
[self.theMovie.view addSubview:nextButton];
[nextButton addTarget:self action:#selector(playNextMovie:) forControlEvents:UIControlEventTouchUpInside];
}
if(currentMovie > 0)
{
UIButton *previousButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
previousButton.frame = CGRectMake(40.0, 138.0, 100.0, 44.0);
[previousButton setTitle:#"Prev" forState:UIControlStateNormal];
[self.theMovie.view addSubview:previousButton];
[previousButton addTarget:self action:#selector(playPreviousMovie:) forControlEvents:UIControlEventTouchUpInside];
}
[[[UIApplication sharedApplication] keyWindow] addSubview: movieView];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(movieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: self.theMovie];}
And here's my code to switch the movie:
- (void) playNextMovie:(id) sender{
currentMovie++;
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:currentMovie];
NSString *videoString = [NSString stringWithFormat:#"%#", [dictionary objectForKey:#"videoFile"]];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:videoString ofType:#"m4v"];
NSLog(#"Video Path: %#", videoPath);
NSURL *moviePath = [NSURL fileURLWithPath: videoPath];
self.theMovie.contentURL = moviePath;
[self.theMovie prepareToPlay];}
Thanks for any help.
It is totally normal for the player to stop playing once a new contentURL is supplied.
The MPMoviePlayerController.view however is usually not being removed automatically. I am assuming that you forgot about one of your notification handlers doing something like that for you.
I am trying to implement video into my app and I am having a hard time. I have tried this tutorial and it was super helpful.
But now when I try to run my app and click on the button, the video frame comes up and just stays black. My video is in the correct format, I also made sure that I am pulling in the MediaPlayer Framework. Has anyone ran into this issue before or knows why this would be happening?
This is what I have:
-(IBAction)playMovie:(id)sender
{
UIButton *playButton = (UIButton *) sender;
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"big-buck-bunny-clip" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)];
[self.view addSubview:moviePlayerController.view];
//moviePlayerController.fullscreen = YES;
moviePlayerController.initialPlaybackTime = 5;
[moviePlayerController play];
}
Have you added MPMoviePlayerController's view onto your view?
MPMoviePlayerController *movieController = ...;
movieController.view.frame = self.view.bounds;
[self.view addSubview:movieController.view];
Frame could also be black if you've set shouldAutoplay to YES and controlStyle to MPMovieControlStyleNone
--Edit
This is how I init my player and it works, maybe that will be of some help. It's playing HTTP Live Streaming video, but it'll play anything you put into it. You should try the url in this sample. If it'll work then there's definitely some problem with your contentUrl.
url = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayerController.view.frame = self.view.bounds;
[self.view insertSubview:moviePlayerController.view atIndex:0];
moviePlayerController.controlStyle = MPMovieControlStyleNone;
moviePlayerController.shouldAutoplay = YES;
[moviePlayerController prepareToPlay];
I had the same problem, and also following the same tutorial, but then I found the class moviePlayerViewController that its easier to use and you don't even have to worry about dismiss buttons.
I fixed used the following code: (I think it works better with ios5)
self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:finalurl];
moviePlayerViewController.view.frame = self.view.bounds;
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
moviePlayerViewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayerViewController.moviePlayer.shouldAutoplay = YES;
[moviePlayerViewController.moviePlayer prepareToPlay];
moviePlayerViewController.moviePlayer.fullscreen=YES;
I have added a movie to my view and it simply starts playing automatically, does anyone know how to stop this?
MPMoviePlayerController *moviePlayer;
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"LAN_S9" ofType:#"mp4"];
if (moviePath)
{
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[moviePlayer view] setFrame: CGRectMake(0, 0, 400, 200)]; // frame must match parent view
}
[self.view addSubview: [moviePlayer view]];
Thats simple:
moviePlayer.shouldAutoplay = NO;
You cannot add a view of MPMoviePlayerController into your view because the MPMoviePlayerController always require full screen mode to play and it will auto jump into that mode.
So, you can extract an image from the film and then show the image with a nice Play button so user can tap on the button to view the video