I am trying to add a movie player on top or inside of a uiview I already have on screen. When I place the movieplayer it is at the top left of the screen. The Uiview I want the movie on is in the Center of the screen on the bottom.
I can play with the numbers and move it down by adding points to the "makeframe" but that doesn't seem correct way of doing this. videoCubeSceneView is the view I want the Movie played on.
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"cubeVideo" ofType:#"mp4"];
NSURL*theurl=[NSURL fileURLWithPath:thePath];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[moviePlayer.view setFrame:CGRectMake(videoCubeSceneView.frame.origin.x, videoCubeSceneView.frame.origin.y, videoCubeSceneView.frame.size.width, videoCubeSceneView.frame.size.height)];
[moviePlayer prepareToPlay];
[moviePlayer play];
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.scalingMode = MPMovieScalingModeFill;
moviePlayer.controlStyle = MPMovieControlStyleDefault;
NSLog(#"url : %#", moviePlayer.contentURL);
[moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
[self.view addSubview:moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackStateDidChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
self.moviePlayer.frame = self.view.bounds;
let try this code
self.moviePlayer.view.center = self.view.center;
[videoCubeScene addSubview:moviePlayer.view];
Add it to the view I actually want it in, then it will be to the coordinates of that said view. When adding it to "self.view" it will add it to the top left of the screen just as it should, because the coordinates are of the main view(The entire screen)
Related
I have a problem while playing a video in full screen mode using MPMoviePlayerViewController (works perfectly in normal mode).
CODE:
if (PlayV) {
self.previewView.hidden=NO;
self.videoController =[[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
[self.videoController.view setFrame:self.previewView .frame];
self.videoController.view.center=self.playBtn.center;
[self.previewView addSubview:self.videoController.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.videoController];
self.videoController.scalingMode = MPMovieScalingModeAspectFit;
self.videoController.controlStyle = MPMovieControlStyleNone;
[self.videoController play];
}
I got the solution. Add this line of code in playBtnAction method:
- (IBAction)playBtnAction:(id)sender {
[self.videoController setFullscreen:YES animated:YES];
}
and then add this line of code in your videoPlayBackDidFinish.
MPMoviePlayerController *player=[notification object];
if ([player respondsToSelector:#selector(setFullscreen:animated:)]) {
[player.view removeFromSuperview];
}
This code will check if the video is in fullscreen mode and bring it back to normal mode.
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:imagepath]]; //moviePlayer declared in .h and set URL
moviePlayer.view.frame = CGRectMake(364, 89, 660, 668); //set custom frame
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view]; //add to view
[moviePlayer setFullscreen:NO animated:YES];
This may help you.
I am using MPMoviePlayerController for playing video files and I am able to apply the natural size of the video with the help of MPMovieNaturalSizeAvailableNotification. But after adding this notification seek bar is not appearing in the controlStyle (all other buttons are present). If I make the video frame hard coded (i.e. without considering the natural height) I will not get this issue.
Here is my code
// Declartion class level
MPMoviePlayerController *moviePlayer;
float naturalHeight;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
moviewPlayer = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieReadyForDisplay:)
name:MPMoviePlayerReadyForDisplayDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieNaturalSizeAvailable:)
name:MPMovieNaturalSizeAvailableNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.fullscreen = NO;
moviePlayer.scalingMode = MPMovieScalingModeFill;
moviePlayer.shouldAutoplay = NO;
//I am able to get the seek bar here if I set the static height . But I want natural height here.
// moviePlayer.frame = CGRectMake(0, 0, self.view.frame.size.width , naturalHeight);
}
-(void) movieNaturalSizeAvailable:(NSNotification *)notification{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMovieNaturalSizeAvailableNotification object:player];
naturalHeight = player.naturalSize.height;
if(naturalHeight > self.view.frame.size.height) {
naturalHeight = self.view.frame.size.height;
}
}
-(void) movieReadyForDisplay:(NSNotification *)notification{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerReadyForDisplayDidChangeNotification object:player];
NSLog(#">> %f ", naturalHeight); //OK here
[player view].frame = CGRectMake(0, 0, self.view.frame.size.width , naturalHeight);
player.view.center = self.view.center;
[player prepareToPlay];
[player play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player stop];
[player.view removeFromSuperview];
}
#end
I believe this issue might be because the natural size calculations of
video player occur after it sets its components frames. If I don't use natural size, it works perfectly. How can I fix this issue?
try this
before starting the video
player.controlStyle = MPMovieControlStyleNone;
and when movie starts playing then set
player.controlStyle = MPMovieControlStyleFullscreen;
You can get play callback from MPMediaPlayback protocol.
I'm working on live streaming of different channels. It work fine when using MPMoviePlayerViewController, but I want to add player as "subview". When I do this it does not work, a black screen come on player. Some time it work fine but after few second it show black again. I don't find any solution. I just want to add two button "Previous channel" and "Next channel" to change channel.Please help.
I am using below code:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[player prepareToPlay];
[player setControlStyle:MPMovieControlStyleEmbedded];
[player setMovieSourceType:MPMovieSourceTypeStreaming];
[player setFullscreen:YES];
[player.view setFrame: self.view.bounds];
player.shouldAutoplay = YES;
[pPlayerView addSubview: player.view];
[player play];
I have the following code to play a video
moviePlayer1 =[[MPMoviePlayerController alloc] initWithContentURL:firstMovieURL1];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doneButtonClicked) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[moviePlayer1 view] setFrame: [self.view bounds]]; // frame must match parent view
[self.view addSubview: [moviePlayer1 view]];
[moviePlayer1 play];
-(void)doneButtonClicked
{
NSLog(#"doneButtonClicked ...");
[moviePlayer1 stop];
[moviePlayer1.view removeFromSuperview];
}
If I use the above code my screen looks like this. I have to click on the little arrow icon and then the screen again to show controls (forward/backwards/done button)
so what I did was add this controlStyle to my code. I see all the buttons now as soon as the player is launched but and now my done button doesn't close the window instead it just pauses the movie. What happened?
moviePlayer1.controlStyle=MPMovieControlStyleFullscreen;
[moviePlayer1 play];
This is how you do it. God forbid if apple makes this logic any easy on us.
[[moviePlayer1 view] setFrame: [self.view bounds]]; // frame must match parent view
[self.view addSubview: [moviePlayer1 view]];
moviePlayer1.controlStyle=MPMovieControlStyleFullscreen;
[moviePlayer1 prepareToPlay];
[moviePlayer1 play];
//use this instead
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doneButtonClicked:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
-(void)doneButtonClicked:(NSNotification*)notification
{
NSLog(#"doneButtonClicked ...");
NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([reason intValue] == MPMovieFinishReasonUserExited)
{
// done button clicked!
[moviePlayer1 stop];
[moviePlayer1.view removeFromSuperview];
}
}
I am writing a simple method to start a streaming video full-screen and letting the user quit pressing the "Done" button. Problem is, I can't remove the MPMediaPlayerController view, or perhaps I am doing it the wrong way.
- (IBAction)playVideoButtonPressed:(id)sender {
NSURL *url = [NSURL URLWithString:#"http://mysite.com/video.mov"];
mp = [[MPMoviePlayerController alloc] initWithContentURL: url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerPlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
if ([mp respondsToSelector:#selector(setFullscreen:animated:)]) {
mp.controlStyle = MPMovieControlStyleFullscreen;
mp.shouldAutoplay = YES;
[self.view addSubview:mp.view];
[mp.view setTag:3];
[mp setFullscreen:YES animated:YES];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mp];
[mp play];
}
- (void)moviePlayBackDidFinish:(id)sender {
NSLog(#"Quitting MoviePlayer");
[mp.view removeFromSuperview];
}
The idea is that the MPMediaPlayerController view is called clicking on a button in the app, and it is dismissed by clicking the "Done" video or when the video ends.
I should have used MPMoviePlayerViewController.