I am playing a video by using MPMoviePlayerController:
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"myvideo.MOV" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0,0, 1024, 675);
[moviePlayer play];
But requirement is that by default video should be in full screen and when I minimize it should be in above frame size.
Please help me out.
Try this.
#define degreesToRadian(x) (M_PI * (x) / 180.0)
-(void)playMovieAtURL:(NSURL*)movieURL
{
if ([NSClassFromString(#"MPMoviePlayerController") instancesRespondToSelector:#selector(view)])
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
// running iOS 3.2 or better
mViewPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
mViewPlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
mViewPlayer.view.frame = newFrame;
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[mViewPlayer.view setTransform: landscapeTransform];
[self.view addSubview:mViewPlayer.view];
[mViewPlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieDidExitFullscreen:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[mViewPlayer moviePlayer]];
#endif
}
else
{
MPMoviePlayerController *mMPPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
mMPPlayer.scalingMode=MPMovieScalingModeFill;
mMPPlayer.backgroundColor=[UIColor blackColor];
[mMPPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMPPlayer];
}
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) movieDidExitFullscreen:(NSNotification*)notification
{
//[[UIApplication sharedApplication] setStatusBarHidden:YES];
/*/ Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];*/
[[NSNotificationCenter defaultCenter] removeObserver: self
name:MPMoviePlayerPlaybackDidFinishNotification
object: [notification object]];
MPMoviePlayerController *theMovie1 = [notification object];
[theMovie1.view removeFromSuperview];
[theMovie1 release];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
//[[UIApplication sharedApplication] setStatusBarHidden:YES];
/*/ Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];*/
[[NSNotificationCenter defaultCenter] removeObserver: self
name:MPMoviePlayerPlaybackDidFinishNotification
object: [notification object]];
MPMoviePlayerController *theMovie1 = [notification object];
[theMovie1.view removeFromSuperview];
[theMovie1 release];
}
Related
I am trying this code but it shows a still image, and not proper video streaming. However it shows the video, but only for some time.
[reykjavikurtjorn loadRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://213.167.154.114/mila/_definst_/tjornin.stream/playlist.m3u8"]]];
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:#"http://www.domain.com/myVideo.mp4"];
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mpvc];
and the delegate
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player respondsToSelector:#selector(setFullscreen:animated:)]) {
[player.view removeFromSuperview];
}
I am using the MPMoviePlayerController to open a video file.The video runs fine.But suppose if the video file is 10 sec ,with 1 sec remaining the video stops.Is that its natural way of playing or should we specify something.The follow is the code used
NSURL *fileURL = [NSURL URLWithString:location];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
self.moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:self.moviePlayerController.view];
- (void)moviePlaybackComplete:(NSNotification *)notification {
NSNumber *finishReason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) {
self.moviePlayerController = [notification object];
self.moviePlayerController.view.hidden = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
[self.moviePlayerController release];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I want to hide the controls from the MPMoviePlayer with this code:
-(IBAction)video:(id)sender {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Intro" ofType:#"mov"];
NSURL *movie = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *control = [[MPMoviePlayerController alloc]initWithContentURL:movie];
//[self.view addSubview: control.view];
control.scalingMode = MPMovieScalingModeFill;
control.controlStyle = MPMovieControlStyleNone;
control.shouldAutoplay = YES;
[control play];
MPMoviePlayerViewController *movieplayer = [[MPMoviePlayerViewController alloc]initWithContentURL:movie];
[self presentMoviePlayerViewControllerAnimated:movieplayer]; }
But that does not work.
You are repeating code. MPMoviePlayerViewController has MPMoviePlayerController. So use it as movieplayervc.moviePlayer.controlStyle = MPMovieControlStyleNone;
have you tried this
[videoPlayerobj setControlStyle:MPMovieControlStyleNone];
My player is set up in the viewDidLoad and this line hides the MPMoviePlayerController. I have intialised my MPMoviePlayer controller as *stream.
stream.view.hidden = YES;
Hope this helps!
You can play video and stop video and remove from your custom view with this code. and MPMoviePlayerController is movie player.
Hope this is useful for you.thank you
-(void)playMovie:(id)sender {
UIButton *buttonThatWasPressed = (UIButton *)sender;
buttonThatWasPressed.enabled = NO;
NSString * str=[[NSBundle mainBundle]pathForResource:#"yo2" ofType:#"mov"];
NSURL * url=[NSURL fileURLWithPath:str];
MPMoviePlayerController * movieController=[[MPMoviePlayerController alloc]initWithContentURL:url];
movieController.controlStyle=MPMovieControlStyleFullscreen;
[movieController.view setFrame:self.view.bounds];
[self.view addSubview:movieController.view];
[movieController prepareToPlay];
[movieController play];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDonePressed:)
name:MPMoviePlayerDidExitFullscreenNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES]; }
This method is called when your video or movie is stop from user or video playback has finish.
-(void) moviePlayBackDonePressed:(NSNotification*)notification {
[_moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
_moviePlayer=nil;
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification { // Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
I have used a MPMoviePlayerController object to play a movie in my app but as soon as I call [moviePlayer play] in a function there is a small jerk and then the movie starts playing.
Edit:
This is the code that I am using.
NSString *urlStr = [[NSBundle mainBundle] pathForResource:sVideoName ofType:nil];
NSURL *movieURL = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.view.frame = CGRectMake(lowerScroll.frame.size.width *(((AppDelegate *)[UIApplication sharedApplication].delegate).currentPage-1), 0.0,1024.0,lowerScroll.frame.size.height);
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.scalingMode = MPMovieScalingModeFill;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.view.backgroundColor = [UIColor clearColor];
moviePlayer.view.alpha = 0;
[lowerScroll addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[self performSelector:#selector(showMovie) withObject:nil afterDelay:0.3];
- (void)showMovie {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[moviePlayer play];
moviePlayer.view.alpha = 1;
[UIView commitAnimations];
}
- (void)movieFinishedCallback:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if (moviePlayer) {
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
moviePlayer = nil;
}
}
Could anybody please tell what could be the reason for the same and how to remove that jerk.
Thanx in advance.
i will like to play a short movie clips in the end of my game in the apps, how will i need to done it ? is there any tutorial on MPMovieplayer ?
thanks for answering my questions
cheers
i currently have these code, how do i play it in the app without poping out the youtube interface ?
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"EndVideo" ofType:#"m4v"]];
MPMoviePlayerViewController * playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release]; playercontroller = nil;
refer
http://stackoverflow.com/questions/5015165/playing-a-video-file-from-server-in-an-iphone-app
I am playing a movie stored on server.Even if you have local file you can convert the path to NSURL & use it
Create a NSObject class CustomMoviePlayerViewController
In the .h file
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface CustomMoviePlayerViewController : UIViewController
{
MPMoviePlayerController *mp;
NSURL *movieURL;
}
- (id)initWithPath:(NSString *)moviePath;
- (void)readyPlayer;
#end
In the .m file
//
// CustomMoviePlayerViewController.m
//
#import "CustomMoviePlayerViewController.h"
#pragma mark -
#pragma mark Compiler Directives & Static Variables
#implementation CustomMoviePlayerViewController
-(void)viewWillDisappear:(BOOL) animated
{
if(mp) {
[mp stop];
}
}
- (id)initWithPath:(NSString *)moviePath
{
mp.controlStyle = MPMovieControlStyleNone;
// Initialize and create movie URL
if (self = [super init])
{
movieURL = [NSURL fileURLWithPath:moviePath];
[movieURL retain];
}
return self;
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
[mp setControlStyle:MPMovieControlStyleFullscreen];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
NSString *deviceModel = [NSString stringWithFormat: #"Device Type: %#\n", [[UIDevice currentDevice] model]];
NSRange range = [deviceModel rangeOfString:#"iPad"];
if(range.location != NSNotFound){
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
}
[[self view] addSubview:[mp view]];
[mp play];
}
}
else
{
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
}
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 1024, 768)];
[[self view] setCenter:CGPointMake(160, 240)];
[[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[[self view] addSubview:[mp view]];
[mp play];
}
}
#endif
}
/*---------------------------------------------------------------------------
* For 3.1.x devices
* For 3.2 and 4.x see moviePlayerLoadStateChanged:
*--------------------------------------------------------------------------*/
- (void) moviePreloadDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[mp play];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self dismissModalViewControllerAnimated:NO];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) readyPlayer
{
//NSLog (#"readyPlayer");
[[UIApplication sharedApplication] setStatusBarHidden:YES];
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
mp.scalingMode= MPMovieScalingModeAspectFit;
if ([mp respondsToSelector:#selector(loadState)])
{
// Set movie player layout
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
//[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setControlStyle:MPMovieControlStyleNone];
[mp setFullscreen:YES];
#endif
file://localhost/Users/ngamacbook/Documents/iPhoneProjectDocs/Team%20Projects/FietIpad_21Feb/Fiet-iPad(05-01-11)
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
#endif
}
else
{
// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) loadView
{
[self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
[[self view] setBackgroundColor:[UIColor blackColor]];
self.view.clipsToBounds = YES;
self.view.autoresizesSubviews = YES;
if(mp) {
[mp stop];
}
UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
}
}
- (void)dealloc
{
[mp release];
[movieURL release];
[super dealloc];
}
#end
whenever you want to play the file
NSString *movieFile=[[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp4"];
// Create custom movie player
moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:movieFile] autorelease];
[self presentModalViewController:moviePlayer animated:NO];
[moviePlayer readyPlayer];
Also add MediaPlayer.framework to your project