I have a MPMoviewPlayerViewController
embedded into an UIView
object. When I start the player in the embedded mode everything works fine and as exp
I had the same problem, but with loading a video from a url (on the web)
Previously I:
MPMoviePlayerPlaybackDidFinishNotification
notificationsMPMoviePlayerViewController
(no content url at this stage)presentMoviePlayerViewControllerAnimated
:MPMoviePlayerViewController
' moviePlayerAs you said, occasionally the MPMoviePlayerViewController
would get stuck and wouldn't dismiss itself when the user taps exit, to fix this, I changed my autoplay order, so the flow became:
MPMoviePlayerPlaybackDidFinishNotification
notificationsMPMoviePlayerViewController
(no content url at this stage)shouldAutoplay
boolean to NOpresentMoviePlayerViewControllerAnimated
:MPMoviePlayerViewController
' moviePlayershouldAutoplay
boolean to YESSince those two changes, I have yet to see the controller get stuck
I had something similar on iOS 6.
Have you tried forcing the player to play after going fullscreen? By calling [MPMoviePlayerController play]
again for instance - this partly solved the problem I had.
Check the exact URL after you set the Content URL of the player. It may contains some illegal characters.
NSLog(@"%@", player.contentURL);
Simulator removes the spaces but the device doesn't. That's what happened to me.
Just add shouldAutoplay boolean to YES after generating the URL It worked for me.
like this:
NSString *path = [[NSBundle mainBundle] pathForResource:videoFileName ofType:@"mp4" inDirectory:nil];
NSURL *movieURL = [NSURL fileURLWithPath:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] init];
player.contentURL = movieURL;
player.controlStyle = MPMovieControlStyleNone;
player.shouldAutoplay = YES;
[player prepareToPlay];
player.fullscreen = YES;
[player.view setFrame:[[[[UIApplication sharedApplication] delegate] window] frame]]; // player's frame must match parent's
[[[[UIApplication sharedApplication] delegate] window] addSubview: player.view];
[player play];
I solved it by myself. As I add the Movie Player as a subview to a container view I don't need to use the actual view controller created with the MPMoviePlayerViewController
which is intended to be used to present it modally or in some other vc hierarchy.
For the single purpose of having a Movie Player view that can be added to some other view as a subview the MPMoviePlayerController
's view property is sufficient.
Until iOS 6 both worked but iOS 6 seems to differ in terms of ressource management/lifetime.
The example project is updated with working code.
I've solved this problem with a different approach. Since the main reason of the problem is iOS 6 calling viewWillDisappear: and/or viewDidDisappear: methods. I thought that maybe iOS also calling the same methods of MPMoviePlayerViewController. So I've created a Category for MPMoviePlayerViewController and implemented viewWillDisappear: and/or viewDidDisappear: methods. Interestingly it works. (by the way this is not recommended by apple)
Here are the codes;
Header (MPMoviePlayerViewController_FullscreenFix.h)
#import <MediaPlayer/MediaPlayer.h>
@interface MPMoviePlayerViewController (MPMoviePlayerViewController_FullscreenFix)
- (void)viewDidDisappear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
@end
Implementation (MPMoviePlayerViewController_FullscreenFix.m)
#import "MPMoviePlayerViewController_FullscreenFix.h"
@implementation MPMoviePlayerViewController (MPMoviePlayerViewController_FullscreenFix)
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
}
@end
Now my code is working on both iOS 6.1.3, 5.5.1 and 4.3.5 versions with the exactly same behavior.