Hide MPMoviePlayerController controls on 1+ plays - ios

I have a MPMoviePlayerController setup:
self.moviePlayerController = [[MPMoviePlayerController alloc] init];
[self.moviePlayerController.view setFrame:frame];
// hide controls initially
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
[self.view addSubview:self.moviePlayerController.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:self.
moviePlayerController];
Then when the notification is fired:
-(void)loadStateChanged:(NSNotification*)notification {
if (self.moviePlayerController.controlStyle == MPMovieControlStyleNone) {
[self.moviePlayerController setControlStyle:MPMovieControlStyleDefault];
}
}
On initial playback this works great, controls are not shown when playback starts but tapping the view shows the controls.
However, when a movie is already playing and I start playback of another URL, I hide the controls, but they reappear after playback starts of the second movie:
[self.moviePlayerController setContentURL:videoURL];
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
[self.moviePlayerController play];
The notification does fire again and it correctly has a controlStyle of MPMovieControlStyleNone which I then set to MPMovieControlStyleDefault.
Anybody have any experience with this, is there a way to make sure the controls stay hidden on a second playback, but are in fact visible when the user taps the movie view.
Thanks!

Related

MPMoviePlayerController stop/freezes video when beginSeekingForward is pressed only on iOS 8+

I am using the MPMoviePlayerController to play some streaming videos of type .m3u from a url. They video player launches perfectly and the video starts playing perfectly as well, but as soon as I press the Seek Forward or Next or beginSeekingForward button the video complete stops/freezes. After that I can click Done to dismiss the player or scrub the progress bar and it will come back to play to video. But the I can't click on the play/pause or at least seems not to do anything.
I have spent quite sometime looking around online for answer or at least a hint so I can tackle this issue in the right direction but not luck. So, I really hope someone could help with this. Btw, this only happens on iOS 8+, currently I am testing it in iOS 8.1.
This is how I create the player and load the video to it.
NSURL *videoURL = [NSURL URLWithString:self.video.flvurl];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: videoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
self.moviePlayer.shouldAutoplay = NO;
self.moviePlayer.fullscreen = YES;
self.moviePlayer.repeatMode = YES;
self.moviePlayer.view.backgroundColor = [UIColor blackColor];
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.moviePlayer.view.frame = self.containerView.frame;
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
I have tried using multiple different notifications to see if I can catch this within one of them, but not luck at all. I have tested all of these notifications so far.. I have added this to the self.moviePlayer and also the self.moviePlayer.view not luck in either of them.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(willResignActive:)
name:UIApplicationWillResignActiveNotification
object:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoStartedPlaying:)
name:MPMoviePlayerNowPlayingMovieDidChangeNotification
object:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoLoadState:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlaybackState:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlaybackUserInfoKey:)
name:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey
object:self.moviePlayer.view];
Well, if you need more relevant code please let me know. I really need some help with this.. Thanks in advance.
This code worked for me, but just to get rid of freezing. Sign up for the MPMoviePlayerLoadStateDidChangeNotification, like you did above. In the callback method, I simply set the movie players contentURL to the original URL I had initialized it with at the start, and set the movie player to play again.
The functionality of this is when you tap the NEXT or BACK buttons in fullscreen, the video simply starts playing again from the start. That may not be ideal since the user might not expect this, but at least it will stop it from freezing. I'm currently trying to find a better workaround.
- (void) videoLoadState:(NSNotification *)notification
{
MPMovieLoadState loadState = moviePlayer.loadState;
if(loadState == MPMovieLoadStateUnknown)
{
NSLog(#"Movie Load state is unknown");
moviePlayer.contentURL = currentlyPlayingVideoURL;
[moviePlayer prepareToPlay];
}
}
If you check the value of MPMoviePlaybackState in this method, it will always be MPMoviePlaybackStateStopped when NEXT or BACK are pressed, so I'm not sure how to handle it...

How to play a video randomly without pressing the play button

I'm new in iPhone Application Development, and now i faced a problem, can any one tell me how can i play a video randomly without pressing the play button. The video should start automatically while the application launch and play randomly without stopping. I don't have any idea how to do this. I don't have any code also. Please help me any one...
Thanks in advanced.
You could implement it like this:
You need a movie player configured with e.g. a file url and add it to your view
NSURL *url = [[NSBundle mainBundle] URLForResource:movieName withExtension:#"mov"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = // set the frame
[self.view addSubview:moviePlayer.view];
Start the movie player
[moviePlayer play];
Listen to the notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleMoviePlayerStatChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
Start the video again
- (void)handleMoviePlayerStatChange:(NSNotification *)note {
NSLog(#"mp playback state %i", moviePlayer.playbackState);
if (moviePlayer.playbackState == MPMoviePlaybackStatePaused) {
[moviePlayer play];
}
}
To hide the video controls set the controlStyle property of the movie player instance:
moviePlayer.controlStyle = MPMovieControlStyleNone;

After exit full screen MPMoviePlayerController set scaling mode to MPMovieScalingModeFill in ios

In my application i play video using mpmovieplayercontroller
first set scaling mode to MPmovieScalingmodefill and display video correct to scalingmode.
then after i view video in full screen and exit full screen then not set scaling mode to
MPmovieScalingmodeFill and display video in defualt mode.
below my code for video playing
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(ExitFullScreen:)
name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[appDelegate.moviePlayerController setContentURL:fileURL];
if ([appDelegate checkDevice])
{
[appDelegate.moviePlayerController.view setFrame:CGRectMake(0,0, 320,463)];
}
else
{
[appDelegate.moviePlayerController.view setFrame:CGRectMake(0,0, 320,375)];
}
[appDelegate.moviePlayerController prepareToPlay];
appDelegate.moviePlayerController.scalingMode=MPMovieScalingModeFill;
appDelegate.moviePlayerController.controlStyle=MPMovieControlStyleDefault;
appDelegate.moviePlayerController.shouldAutoplay=NO;
[appDelegate.moviePlayerController setFullscreen:YES animated:YES];
[appDelegate.moviePlayerController play];
[self.view addSubview:appDelegate.moviePlayerController.view];
- (void)ExitFullScreen:(NSNotification *)notification{
NSLog(#"Exit full Screen");
[appDelegate.moviePlayerController setControlStyle:MPMovieControlStyleEmbedded];
[appDelegate.moviePlayerController setScalingMode:MPMovieScalingModeFill];}
so my probleem is how can set scaling mode after exit full screen or do not change scaling mode after exit screen ?
please help me out.
thanks.
This isn't the "ideal" solution, but it works!
Basically, once you exit full screen the MPMoviePlayerController instance gets all screwed up and resetting the scaling property to MPMovieScalingModeFill won't help no matter where or when you do it (I've tried all sorts of stuff and after an hour gave up). Easiest solution is to remove the MPMoviePlayerController and simply allocate a new instance of MPMoviePlayerController each time full screen is exited (not ideal, but totally works):
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:NO];
if (self.moviePlayer != nil)
[self.moviePlayer.view removeFromSuperview];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
self.moviePlayer.view.frame = CGRectMake(#, #, #, #);
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.shouldAutoplay = NO;
[self.moviePlayer setContentURL:self.videoURL];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setScalingMode:MPMovieScalingModeFill];
[self.view addSubview:self.moviePlayer.view];
}
PS: Don't forget to call super's viewDidAppear or suffer all sorts of unforeseeable mayhem (a very common mistake in iOS development)
I believe this will generate the MPMoviePlayerScalingModeDidChangeNotification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieScalingModeDidChange:)
name:MPMoviePlayerScalingModeDidChangeNotification
object:nil];
MPMoviePlayerScalingModeDidChangeNotification
Posted when the scaling mode of a movie player has changed. There is no userInfo dictionary. Scaling mode can change programmatically or by user interaction. To set or retrieve the scaling mode of a movie player, access its scalingMode property. The movie player whose state has changed is available as the object associated with the notification.

How can I remove "Loading..." label from MPMoviePlayerViewController?

I have a MPMoviePlayerViewController, I don't want to initialize it with a content url, I just want to have a empty movie player GUI out there. Whenever I want it to load a movie, it then starts loading. But I can't
self.playerViewController = [[MPMoviePlayerViewController alloc] init];
MPMoviePlayerController *player = [self.playerViewController moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
player.shouldAutoplay = FALSE;
player.initialPlaybackTime = 0;
[player setFullscreen:FALSE];
[player.view setFrame:CGRectMake(0, -20, self.view.bounds.size.width, self.view.bounds.size.height/2)];
[self.view addSubview:player.view];
As you see, I init the MPMoviePlayerViewController with no content url, and I set the autoplay to false, but when I run the application. Even though there is no video url given, I can still see a label "Loading ..." near the "Done" button, why???? How can I remove the loading label?
Just use MPMoviePlayerController instead of MPMoviePlayerViewController. You'll need to build a view controller around it if you want to display it modally.
You could also maybe iterate through [MPMoviePlayerViewController view].subviews until you find the UILabel, but that approach might break in future versions of iOS.

iOS: MPMoviePlayerController Done button doesn't work

I am adding an MPMoviePlayerController to a view like so:
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
player.controlStyle = MPMovieControlStyleNone;
[player.view setFrame:self.playerView.bounds];
[self.playerView addSubview:player.view];
self.playerView is a small view inside my main view and I have custom buttons that control playback within that same main view. This all works fine.
I have a fullscreen button that works like so:
- (IBAction) btnFullScreenPressed:(id)sender {
[player setFullscreen:TRUE animated:TRUE];
[player setControlStyle:MPMovieControlStyleFullscreen];
}
This works fine, but then when I hit the Done button on the full screen controls, the movie stops playing but does not return to the smaller self.playerView in my view. How can I get it to "un-fullscreen" and return to the smaller self.playerView?
Thanks.
Quite unintuitively you actually have to set the control style to default, i.e.:
- (IBAction) btnFullScreenPressed:(id)sender {
[player setFullscreen:TRUE animated:TRUE];
[player setControlStyle:MPMovieControlStyleDefault];
}
and, of course, then set it back to none when you receive MPMoviePlayerWillExitFullscreenNotification or MPMoviePlayerDidExitFullscreenNotification (I prefer "did exit").

Resources