MPMovieplayerController "Done" button - iphone

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.

Related

play video in ios6

When the aplicattion starts, the video will run automatically
but pressing the button home and open again the app freezes and do not know why
what can i do?
- (void)viewDidLoad{
m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
[m_player.view setBackgroundColor:[UIColor blackColor]];
[m_player setControlStyle:MPMovieControlStyleNone];
[[m_player view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[m_player play];
[self.view addSubview:[m_player view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[m_player.view removeFromSuperview];
[m_player stop];
m_player = nil;
}
}
This code help me to avoid freezes app when press button home and the video is play and works!!!
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(handleEnteredBackground:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
-(void)handleEnteredBackground:(NSNotification*)_notification{
[m_player play];
}

How to play a video like you tube player in iphone sdk?

I am newbie for ios development, I am working on a project ,where i need to show some sample videos.
i Would like to show the video in streaming format(youtube player) .
-(void) playVideoOfURL:(NSString*)videoPath
{
NSString *url = [NSString stringWithFormat:#"%#%#",TipsService,videoPath];
moviePlayer = [[ MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
if (moviePlayer)
{
moviePlayer.view.frame = CGRectMake(0, 0, 320, 460);
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.useApplicationAudioSession = YES;
//Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
//setup device rotation notification observer
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
UIViewController *player = [[UIViewController alloc] init];
player.view = moviePlayer.view;
[self.navigationController pushViewController:player animated:YES];
[moviePlayer play];
[moviePlayer setFullscreen:TRUE];
}
}
I tried this one, but no use. Please help me...
Try this
[moviePlayer play];
[moviePlayer setFullscreen:TRUE];
UIViewController *player = [[UIViewController alloc] init];
player.view = moviePlayer.view;
[self.navigationController pushViewController:player animated:YES];

MPMoviePlayerController does not remove view when clicking done

I am creating a MPMoviePlayerController object and streaming a video in full screen mode.
I am using a UIViewController to display the movie view.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//http://www.youtube.com/watch?feature=player_detailpage&v=ebeQaznNcmE
NSURL *url = [NSURL URLWithString:#"http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4"];
MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
mPlayer.view.frame = gMainView.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mPlayer];
mPlayer.shouldAutoplay = YES;
mPlayer.controlStyle = MPMovieControlStyleFullscreen;
[gMainView addSubview:mPlayer.view];
[mPlayer prepareToPlay];
[mPlayer setFullscreen:YES animated:YES];
[mPlayer play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
//movie finished playing
}
else if (reason == MPMovieFinishReasonUserExited) {
//user hit the done button
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
}
else if (reason == MPMovieFinishReasonPlaybackError) {
//error
}
}
When clicking done, the video visual is removed from the screen, but the controls are not removed from the screen and the view is not removed from the screen.
The control does come to "//user hit the done button". It does execute the code to remove the view from superview, I checked by adding logs, but the controls are not removed from the screen and the view is not removed from the screen.
What am I doing wrong?
EDIT:
If I use MPMoviePlayerViewController then it doesn't even wait for me to press Done. Once the video is complete it automatically removes the view. But I don' want that.
EDIT:
If I remove "[mPlayer setFullscreen:YES animated:YES]" then when clicking on Done, the view is removed completely. But the video is not displayed in full screen and the status bar goes gray which is again what I don't want.
The below code worked for me, Hope it helps you too.
-(IBAction)playVedio:(id)sender{
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentMoviePlayerViewControllerAnimated:mp];
}
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[mp.moviePlayer stop];
mp = nil;
[mp release];
[self dismissMoviePlayerViewControllerAnimated];
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
id presentedViewController = [window.rootViewController presentedViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if (window && [className isEqualToString:#"AVFullScreenViewController"]) {
return UIInterfaceOrientationMaskAll;
} else {
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
}
else if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
}
return UIInterfaceOrientationMaskPortrait;
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize size = frame.size;
NSLog(#"%#", [NSString stringWithFormat:#"Rotation: %s [w=%f, h=%f]",
UIInterfaceOrientationIsPortrait(interfaceOrientation) ? "Portrait" : "Landscape",
size.width, size.height]);
}
}

MPMoviePlayerViewController becomes black when enters background

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

How can I change the "Loading Movie..." message in MPMoviePlayerController?

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];
...
}