When I launch an instance of MPMoviePlayerController for a remote URL, the top bar displays "Loading Movie..." - is there a way to change this message to a custom one?
You can simply create a UIImageView with an image that you want to display (or label or whatever else) and add it to your MoviePlayerControllerView.
UIImage *loadingScreenImage = [UIImage imageNamed:#"loadingScreen.png"];
loadingScreen = [[UIImageView alloc] initWithImage:loadingScreenImage]; // ivar & property are declared in the interface file
[self.view addSubview:loadingScreen];
[loadingScreen release];
Then you can instantiate the movie player and register to receive a notification when loadState changes:
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movie.trailerURL];
if ([moviePlayer respondsToSelector:#selector(loadState)]) {
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];}
Then in your notification method, do the logic to add the player to the view:
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
// Unless state is unknown, start playback
switch ([moviePlayer loadState]) {
case MPMovieLoadStateUnknown:
break;
case MPMovieLoadStatePlayable:
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
// Set frame of movie player
[moviePlayer.view setFrame:CGRectMake(0, 0, 480, 320)];
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:[moviePlayer view]];
// Play the movie
[moviePlayer play];
...
}
Related
I am using the Notification Center to receive the MPMoviePlayerPlaybackDidFinishNotification, which works as expected.
NSURL * url = [[NSURL alloc] initWithString:#"http://INFINITECREATIVEUNIVERSE.COM/Media/module_1_Dec/c_January27AffactPerfect.mov"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(0, 0, 500, 260)];
[self.view addSubview:moviePlayer.view];
//Some addiontal customization
moviePlayer.fullscreen = YES;
moviePlayer.allowsAirPlay = YES;
moviePlayer.shouldAutoplay = YES;
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
//moviePlayer.endPlaybackTime = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMovieFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
The selector does what I would like it to do.
-(void)playMovieFinished:(NSNotification*)theNotification
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Change Segue ID Here
[self performSegueWithIdentifier:#"VC_5" sender:self];
}
My problem occurs when I click next button while the video is still playing. When my next ViewController appears there is no video and I can still hear the audio from the previous ViewController.
My question is:
How do I properly dismiss the View Controller and the MPMoviePlayer.
I am lost.
I downloaded the example MoviePlayer project from Apple, but it is now deprecated and it does not work properly.
I am lost on how to make this work.
I am crossing my fingers that I have asked the question correctly. Please have mercy on my novice question. Thank you robert
use this code
-(void)playMovieFinished:(NSNotification*)theNotification
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer stop];
// Change Segue ID Here
[self performSegueWithIdentifier:#"VC_5" sender:self];
}
I have been able to successfully play a video on start of the app by calling a function that plays the video in the viewDidLoad method. But on doing so, i am unable to see the "Done" button. The "Done" button only seems to appear if i call that same function with the help of a button. I am posting the code below. How will I make this done appear by calling the same function in the viewDidLoad method? What have i been doing wrong? Thank you!
in my viewcontroller.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMedia];
}
- (void) playMedia {
//hide status bar first
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//resets again in playMediaFinished
movieFile = [[NSBundle mainBundle] pathForResource:#"sec" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath: movieFile];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setFullscreen:NO animated:YES];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
CGAffineTransform transform = self.view.transform;
[moviePlayer.view setFrame:CGRectMake(0, 0, 480, 320)];
moviePlayer.shouldAutoplay = YES;
NSLog(#"Should be playing Movie");
}
- (void) playMediaFinished: (NSNotification*) theNotification {
moviePlayer = [theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer
respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
[playerView removeFromSuperview];
//reset status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
NSLog(#"Should be DONE playing Movie");
}
Try doing this using viewDidAppear instead of viewDidLoad. I'm not surprised starting a video in viewDidLoad is causing unexpected behavior considering you're presenting the video before the main view appears on screen. Keep in mind, the view is done loading before it is actually visible.
I have problem with MPMoviePlayerViewController , when app enters background and then I launch it again or go another viewControllers the movie became black ! I have movie which plays in the background of my menus , here is my code :
EIDTED CODE :
-(void)viewDidLoad {
[self moviePlayer2];
}
- (void) moviePlayer2 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"cloud" ofType:#"mp4"];
player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.view.userInteractionEnabled = YES;
player.moviePlayer.repeatMode = YES;
player.moviePlayer.scalingMode = MPMovieScalingModeFill;
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackStateChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:[player moviePlayer]];
[[player moviePlayer] play];
[self.view addSubview:player.view];
}
-(void) moviePlayBackStateChange: (NSNotification *) note {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:[player moviePlayer]];
[[player moviePlayer] play];
//[player release];
NSLog(#"FINISHED");
}
thank you .
I think you may need to add codes below:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackStateChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:[player moviePlayer]];
and handle the movie state in the moviePlayBackStateChange method.
The movie will be paused when the movie is playing and the app enters in background, so you need to make the movie resume like below when the app come back from background. If not,the movie will keep the pause state. That's why your app becomes black.
[[player moviePlayer] play];
then the movie will continue to play.
adding two methods which you should invote when the app comes into background and backs from background:
-(void) pauseMovieInBackGround
{
[player moviePlayer] pause];
[player.view removeFromSuperview];
}
-(void) resumeMovieInFrontGround
{
[self.view addSubview:player.view];
[[player moviePlayer] play];
}
Hope this can help you guy.
Try changing this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[player moviePlayer]];
[player release];
To this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[player moviePlayer]];
[movieController.view removeFromSuperview];
[player release];
See if that works :D
My Video works when I click my play button, and I know the code for my view change works. I just can't seem to get the view to change after my video is done playing, not sure what is going wrong. Any ideas to get the view to change once the video is done playing?
My code to play movie
-(IBAction)playMovie:(id)sender {
NSString *movieUrl = [[NSBundle mainBundle] pathForResource:#"Movie_1666" ofType:#"m4v"];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:movieUrl]];
//Smoothe Transition
//[self presentMoviePlayerViewControllerAnimated:playerController];
//Instant Transistion
[self presentModalViewController:playerController animated:NO];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playerController.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerController]; }
My method to change views
- (void)playbackFinished:(NSNotification*) notification {
MPMoviePlayerController *playerController = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerController];
View2 *view2 = [[View2 alloc] initWithNibName:#"View2" bundle:nil];
View2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:view2 animated:YES]; }
EDIT:
My Notification object was wrong and wasn't triggering my playbackFinished method
this change fixes that.
- (void)playbackFinished:(NSNotification*) notification {
playerController = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerController];
I also put this in my header file to make it global for use in my playbackFinished method
MPMoviePlayerViewController *playerController;
Once the movie play finished, dismiss the playerController view without animation. Also check, whether you are presenting the View2 in main thread.
Edit:
Dismiss the playerController by
[playerController dismissModalViewControllerAnimated:NO];
I am having problem with MPMovieplayerController .
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
now when I try to write mp. the list does not contain the "setControlStyle" ! is there any thing wrong ?
and if I write directly [mp setControlStyle:MPMovieControlStyleFullscreen]; than video starts in full screen but done button is not displayed but ya if I click on the place where done button should be than it works fine !!!!
EDITED :
- (id)initWithPath:(NSString *)moviePath
{
// Initialize and create movie URL
if (self = [super init])
{
movieURL = [NSURL URLWithString:moviePath];
[movieURL retain];
}
return self;
}
- (void) readyPlayer
{
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
if ([mp respondsToSelector:#selector(loadState)])
{
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else
{
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
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]];
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
//[self makeButton];
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movieplayer
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
need guidance.
As the property in question is called controlStyle, you could use mp.controlStyle=..; or [mp setControlStyle:..];
For the second question / issue, see mpmovieplayercontroller-audio-show-done-button for a quick workaround solution.