I have an universal application that plays movies from the internet. It has to support 3.1.x as well as 4.x.
In order to get this to work, I have a branch in the code that detects pre-3.2 devices and utilizes MPMoviePlayerController as it is supposed to work there.
This is how I prepare the player to play the remote movie:
- (void)registerForMovieNotifications {
//for 3.2 devices and above
if ([moviePlayer respondsToSelector:#selector(loadState)]) {
LOG(#"moviePlayer responds to loadState, this is a 3.2+ device");
//register the notification that the movie is ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didExitFullScreen:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
LOG(#"preparing moviePlayer...");
[moviePlayer prepareToPlay];
} else {
//for pre-3.2 devices
LOG(#"This is a 3.1.x device");
//register the notification that the movie is ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
//handle when the movie finished
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)readyPlayer {
if (!moviePlayer) {
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
} else {
[moviePlayer setContentURL:movieURL];
}
[self registerForMovieNotifications];
}
Later on I get this notification, and it sets up the movie player's view, etc.
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
LOG(#"3.2/4.x - moviePlayerLoadStateChanged:");
//unless state is unknown, start playback
if ([moviePlayer loadState] != MPMovieLoadStateUnknown) {
//remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
//set the frame of the movie player to match
self.view.autoresizesSubviews = YES;
[[moviePlayer view] setFrame:self.view.bounds];
[[moviePlayer view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[moviePlayer view] setAutoresizesSubviews:YES];
//add movie player as a subview
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES];
//play the movie
[moviePlayer play];
}
}
And the movie plays. This works perfectly on iPhone 4.2, 4.3, iPad 4.2, 4.3, but it fails on iPad 3.2. The movie plays but all I get is a black screen.
If I remove the [moviePlayer setFullscreen:YES] call I get a visible playing movie in 3.2, however it isn't "fullscreen" and so it doesn't have the Done button and there's no way for me to dismiss the screen.
I'd love some help on what's going on here. Thanks!
I was able to come to an acceptable solution, but I still feel this might be a bug.
If I skip the call to setFullScreen and instead just manually set the controlStyle to MPMovieControlStyleFullScreen then it gives me a view that is mostly correct (the toolbar is about 40 pixels too low).
Then I can get the Done button, which triggers the moviePlayer:didFinishPlaying callback.
So it stinks I now have a smelly if 3.2 branch of logic in my code, but hopefully most people will be on 4.0 anyway.
Related
I was able to play .mov file using following coding in ios 6.1.3 and below but in iOS7 it closed automatically.
MPMoviePlayerViewController *mp1 = [[ MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
if (mp1)
{
self.moviePlayer= mp1;
[mp1 release];
self.moviePlayer.view.userInteractionEnabled=YES;
self.moviePlayer.moviePlayer.repeatMode = MPMovieRepeatModeOne;
self.moviePlayer.view.frame = CGRectMake(0, 0, 320, 460);
[self.moviePlayer.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
self.moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleDefault;
//Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerDidExitFullscreenNotification object:self.moviePlayer.moviePlayer];
//setup device rotation notification observer
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[self.masterVC presentMoviePlayerViewControllerAnimated:moviePlayer];
[self.moviePlayer.moviePlayer prepareToPlay];
[self.moviePlayer.moviePlayer play];
[self.moviePlayer.moviePlayer setFullscreen:TRUE];
}
Using above coding I can able to play .mp4 file in iOS7 and all. Does apple restricted mov files?
I may be having the same issue as you. If you look at your logs, do you get the following error:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
The only other idea I have is to use a new class such as AVPlayer or even a third party class to play my media. I don't see any settings that are incorrect in your code nor my code. It is iOS 7 related though.
I want to create an animated logo that serves as the splash screen for my iphone/ipad app.
I'm thinking of showing the default.png, which then transitions to an .mp4 (where the first frame of the .mp4 matches the default.png), plays a 3 second movie, fades out, and loads my application.
Does anyone have any experience with this? And is my idea (using .mp4) the best way to achieve this? Also, is Apple "cool" with this?
Thanks in advance.
Yes you can absolutely do this and yes Apple is cool with it.
You could use MPMoviePlayerController, place it under a UIImageView with the launch image and when the movie is loaded and ready to go remove the UIImageView and play the movie.
But given the sometimes finicky nature of MPMoviePlayerController you need to time things carefully. Here's a snippet you can use as a starting point:
-(void)setupMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification object:self.playerView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:)
name:MPMoviePlayerLoadStateDidChangeNotification object:self.playerView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showMovie:)
name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.playerView];
[self.playerView setContentURL:[[NSBundle mainBundle] URLForResource:#"movie" withExtension:#"mov"]];
[self.playerView prepareToPlay];
}
-(void)playMovie:(NSNotification *)notification {
if (self.playerView.loadState == MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:notification.object];
[self.playerView play];
}
}
-(void)showMovie:(NSNotification *)notification {
if (self.playerView.playbackState == 1) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:notification.object];
// should update this to new UIView anim methods
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.splashScreen.alpha = 0;
[UIView commitAnimations];
}
}
I think that a tutorial like this one can be helpful: http://www.youtube.com/watch?v=wCsumlHiEc0&feature=channel_video_title
Respond to the UIApplicationDidFinishLaunchingNotification. I agree with #jhocking that you should consider whether such a wait is the best UX, but if it is, it's a pretty straightforward task.
I am playing a video by default in full screen according to this:
Play video by default in full screen
But using this code minimize control is missing.
My exact requirment is that:
As the view will load a video will play by default in full screen and when it will be minimize it should be play in a certain frame.
And when it will end I want to write some code, but What condition will be apply to check to whether the video is finish/end?
Plz help me out.
Thanks.
when ever you alloc your moviePlayer object add bellow notification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
so when your video will finish playing or you will finish it by done bellow method will be called:
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
// write your code here
}
You need to register for the notification as below
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieLoaded:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
and then implement the messages as follows:
- (void)movieLoaded:(NSNotification*)notification
- (void)moviePlayBackDidFinish:(NSNotification*)notification
hi use this its work fine
AVAsset *aset=[AVAsset assetWithURL:url];
AVPlayerItem *item=[[AVPlayerItem alloc]initWithAsset:aset];
play=[[AVPlayer alloc]initWithPlayerItem:item];
AVPlayerLayer *layer=[[AVPlayerLayer alloc]init];
layer.player=play;;
layer.frame=CGRectMake(200, 250, 400, 250);
[self.view.layer addSublayer:layer];
[play play];
I have an existing iPhone/iPad universal app that streamed video with the MPMoviePlayerController over wi-fi (mp4) and 3G Network (3gp). When iOS 4.3 was released our 4.3 devices can no longer play a 3gp video over 3G network. I tested a local 3gp file and that fails too, but works on a 3G phone running 4.2.x.
After debugging the view controller I see the MPMoviePlayerLoadStateDidChangeNotification notification is not being triggered when trying to play the 3gp file locally or streaming it via URL.
[APPDEL showStatusView: #"Please wait..."];
NSString * videoUrl = [[request.URL description] stringByReplacingOccurrencesOfString: #"idvideo:" withString: #"http:"];
DLog(#"Loading video %#", videoUrl);
_moviePlayer = [[IDMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString: videoUrl]];
_moviePlayer.view.backgroundColor = [UIColor clearColor];
_moviePlayer.allowsAirPlay = YES;
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
if ([_moviePlayer respondsToSelector:#selector(loadState)]) {
// May help to reduce latency
[_moviePlayer prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerPreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:_moviePlayer];
//[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(moviePlayerTimerUp:) userInfo:nil repeats:NO];
}
else {
// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(onMovieDone:) name: MPMoviePlayerPlaybackDidFinishNotification object: nil];
if ([_moviePlayer respondsToSelector: #selector(view)]) {
[self.view addSubview: _moviePlayer.view];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
_moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
}
else {
_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
}
_moviePlayer.view.frame = _webView.frame;
}
[_moviePlayer play];
The first condition for the loadState is always hit. For the 3gp file the code will never reach the moviePlayerLoadStateChanged selector method which hides an indicator/status view and creates other fullscreen/orientation notifications. The indicator/status view will just remain up & spinning. Within debugger I can see the onMovieDone selector method gets triggered far sooner than the video would of really of finished.
Confused.
This my answer in regards with playing .3gp file with iPhone .....I have successfully played .3gp file with iphone sdk with using MpMoviePlayerController and my answer link is:
Link: https://stackoverflow.com/a/13088808/1092219
hope you will got help from my answer.........!! :))))))
The code below is more or less taken from the example MPMoviePlayerController sample code. In an app I wrote last year, it used to play videos fullscreen without an issue. Since iOS 4.0, there's just audio in the background. It's like the movie player doesn't have a view or the view is behind my app. I can still interact with my app, even 'start' a new video (audio only).
It's like the movie player now needs a view, but I don't see any way of supplying this in the API or the sample code (which does seem to be a version or two behind.
I load my videos from a URL and if I type these into Safari, they play just fine.
Here's the relevant code fragments, for what it's worth:
- (void)playMovieUrl:(NSURL*)url
delegate:(id)delegate
callbackSelector:(SEL)selector
{
#try {
movieFinishedCallbackDelegate = delegate;
movieFinishedCallbackSelector = selector;
movieURL = url;
MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie play];
}
#catch (NSException * e) {
return;
}
}
// When the movie is done,release the controller.
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
MPMoviePlayerController* theMovie=[aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie release];
[movieURL release];
[movieFinishedCallbackDelegate performSelector:movieFinishedCallbackSelector];
}
You probably need to present theMovie:
[self presentMoviePlayerViewControllerAnimated:theMovie];
And change to:
MPMoviePlayerViewController
in ios 3.2 letter use MPMoviePlayerViewController. it behave like a modelViewcontroller