The following is the code I used to play a movie:
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
float screenWidth = self.view.frame.size.width;
float screenHeight = self.view.frame.size.height;
[playerViewController.view setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[self.view addSubview:playerViewController.view];
[self.view setUserInteractionEnabled:YES];
//---play movie---
player = [playerViewController moviePlayer];
[player setControlStyle:MPMovieControlStyleNone];
[player setFullscreen:TRUE];
[player play];
skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
[skipButton setTitle:#"Skip" forState:UIControlStateNormal];
skipButton.frame = CGRectMake(0, 0, 150, 50);
[skipButton setCenter:CGPointMake(screenWidth - 30 - skipButton.frame.size.width, 100)];
[skipButton addTarget:self action:#selector(skipMovie) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:skipButton];
But, I have some issue with these on iPad1. If I did not use the setFullScreen method, the movie didn't play in FullScreen mode, even if I set the Rect (1024x768). But if I set these, the movie does play in FullScreen, but my #skipButton is not visible.
If i use:
[self.view addSubview:playerViewController.view];
after
[player play];
the first issue happened. The code works properly in iPad2, even without setFullScreen.
Does anyone have any ideas?
[playerViewController.view bringSubviewToFront: skipButton]
doesn't make any change!!!!
I think you can solve this issue by using this method
[playerViewController.view bringSubviewToFront: skipButton];
Please call this function only after your movie player loads the movie.
Related
I was looking for answer in other questions but found out it's impossible to play video in iPhone in a view and not fullscreen, but maybe it has changed with the new versions? Somebody knows anything?
Use MPMoviePlayerController for playing the video and set the frame where you want to display it in the layout.
// Create custom movie player
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:URL];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(onMSAASDone:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[moviePlayer setFullscreen:FALSE];
//---play partial screen---
moviePlayer.view.frame = CGRectMake(0, 0, 200, 300);
[self.view addSubview:moviePlayer.view];
I am currently working on an app in which I need to play a video. Everything is working fine. But if my application is sent to the background and then again to the foreground it shows a solid black screen.
Please provide me with a solution to this problem.
[player.view setFrame:CGRectMake(0, 0, 480, 278)];
[player.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
outputURL = [NSURL fileURLWithPath:filePath];
[player setContentURL:outputURL];
[player setShouldAutoplay:NO];
player.repeatMode = MPMovieRepeatModeNone;
player.controlStyle = MPMovieControlStyleNone ;
player.scalingMode = MPMovieScalingModeAspectFit;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player prepareToPlay];
[player pause];
[self.view addSubview: player.view];
Just see this solution
For MPMoviePlayer
I am using MPMoviePlayerController for playing the media . I want to set the image at the background of the player.The image is set accordingly but when i set the bounds for the image then the image is not set according to bounds . I have tried following code:
UIImageView *imageView=[[UIImageView alloc]initWithImage:[operationControl getCoverImage:stringId]];
imageView.bounds=CGRectMake(0, 100, 200, 200);
[moviePlayer.backgroundView addSubview:imageView];
[moviePlayer.backgroundView setBackgroundColor:[UIColor purpleColor]];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
I am using MPMoviePlayerController in my UIView and the goal is to put it there on View as inline view. The problem is that the code doesn't work except the full screen.
-(IBAction)startVideo {
//start video here
NSURL *path = [[NSURL alloc] initWithString:[self localVideoPath:NO]];
// Create custom movie player
MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:path] autorelease];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setFullscreen:FALSE];
// May help to reduce latency
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(onMSAASDone:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
//---play partial screen---
//moviePlayer.view.frame = CGRectMake(0, 0, 200, 300);
moviePlayer.view.frame = image.frame;
//[[moviePlayer view] setFrame: [image bounds]];
[image removeFromSuperview];
[self.view addSubview:moviePlayer.view];
// Show the movie player as modal
//[self presentModalViewController:moviePlayer animated:YES];
// Prep and play the movie
[moviePlayer play];
}
The sample code from Apple is flawed or let's say outdated. You need to add the moviePlayer's view as subview to your view. Something like this:
MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:path] autorelease];
...
// Adjust positioning where I used the bound of the outer view (of type UIView)
moviePlayer.view.frame = outerView.bounds;
// Now add the movie player to the outer view
[outerView addSubView:moviePlayer.view];
...
This should do the trick.
Sorry, I did not see that you added the subview already.
Okay, for a sample code you can take the XCode sample project called MoviePlayer_iPhone (inside the XCode documentation for MPMoviePlayerController you'll find a link for the MoviePlayer sample project) and just adjust AppDelegate's initAndPlayMovie this way:
-(void)initAndPlayMovie:(NSURL *)movieURL
{
// Initialize a movie player object with the specified URL
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
// save the movie player object
self.moviePlayer = mp;
[mp release];
// Apply the user specified settings to the movie player object
[self setMoviePlayerUserSettings];
self.moviePlayer.view.frame = self.window.bounds;
[self.window addSubview:self.moviePlayer.view];
// Play the movie!
[self.moviePlayer play];
}
}
This one is crude because it does not set the frame or centers the view but it should display the movie when you go to local and click on Play Movie.
The only drawback I saw was that the fullscreen is not going black. That said the sample project is pretty weird and not very well written. That said it displays the non-fullscreen video.
I've the requirement to show video as splash screen in my iphone app
I'm using following code:
-(void)setupMovie{
NSString* moviePath = [[NSBundle mainBundle] pathForResource:#"iphone" ofType:#"mp4"];
NSURL* movieURL = [NSURL fileURLWithPath:moviePath];
playerCtrl = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
playerCtrl.scalingMode = MPMovieScalingModeFill;
playerCtrl.controlStyle = MPMovieControlStyleNone;
playerCtrl.view.frame = CGRectMake(0, 0, 480, 320);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[playerCtrl.view setCenter:CGPointMake(240, 160)];
[playerCtrl.view setFrame:CGRectMake(0, 0, 480, 320)];
[playerCtrl.view setTransform:CGAffineTransformMakeRotation(-M_PI/2)];
[self.window addSubview:playerCtrl.view];
[playerCtrl play];}
But the player view is not starting at x=0,y=0 location. What's the reason and how to fix it.
Its starting x=100,y=100 (approx).
you are setting the information which affects the position three times
playerCtrl.view.frame = CGRectMake(0, 0, 480, 320);
[playerCtrl.view setCenter:CGPointMake(240, 160)];
[playerCtrl.view setFrame:CGRectMake(0, 0, 480, 320)];
so try one after another
Set frame after rotation:
playerCtrl.view.frame = CGRectMake(0, 0, 320, 480);