maxgrinev
maxgrinev

Reputation: 281

Cannot play video with MPMoviePlayerViewController

I created a fresh project with the following ViewController.m. When I run the app I can see a box of the expected origin/size (38, 100, 250, 163) but it is black and no video playing. There is a strange output in Xcode:

2012-08-23 15:36:45.559 VideoTest1[11398:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2012-08-23 15:36:45.560 VideoTest1[11398:c07] [MPAVController] Autoplay: Disabling autoplay
2012-08-23 15:37:18.186 VideoTest1[11398:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)

Note that the video is converted with Videora iPhone Converter and plays ok in Xcode (so it is not a video problem); the path to the video is ok because when I specify demo-iPhone1 (which does not exist) I get a nil exception. I tried in Simulator and on iPhone: always black box. Any ideas?

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>  

@interface ViewController ()

@end

@implementation ViewController
- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];

    [moviePlayerController.view removeFromSuperview];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"demo-iPhone" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];
    [moviePlayerController.view setFrame:CGRectMake(38,
                                                    100,
                                                    250,
                                                    163)];
    [self.view addSubview:moviePlayerController.view];
    [moviePlayerController play];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

Upvotes: 8

Views: 10325

Answers (4)

Andrew McKinley
Andrew McKinley

Reputation: 1137

Also check out the video format. I kept getting this error with my sample video .m4v (which I downloaded off of Apple's website). Eventually I tried it with a different video clip that was .mp4 and it worked fine. Many of the errors still popped up on my console.

2013-01-22 15:44:04.850 VideoTesting[4497:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-22 15:44:04.851 VideoTesting[4497:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2013-01-22 15:44:04.853 VideoTesting[4497:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2013-01-22 15:44:04.853 VideoTesting[4497:c07] [MPAVController] Autoplay: Disabling autoplay
2013-01-22 15:44:04.861 VideoTesting[4497:c07] [MPAVController] Autoplay: Enabling autoplay

The video still played however.

Upvotes: -2

vmanjz
vmanjz

Reputation: 1159

I just solved a similar problem with this line of code. The player controllers now shows up and the video plays perfectly:

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
//
@synthesize moviePlayer = _moviePlayer;
//
[self.moviePlayer prepareToPlay];

Modify to fit your environment.

Upvotes: 13

Andreas Grauel
Andreas Grauel

Reputation: 155

Do you use ARC? If so you have to retain the MPMoviePlayerController!

Add this to your interface

@property (nonatomic, strong) MPMoviePlayerController *controller;

Check last line of viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"demo-iPhone" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                           name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayerController];
    [moviePlayerController.view setFrame:CGRectMake(38,
                                                100,
                                                250,
                                                163)];
    [self.view addSubview:moviePlayerController.view];
    [moviePlayerController play];
    [self setController:moviePlayerController];
}

Upvotes: 2

BabyPanda
BabyPanda

Reputation: 1592

I solve a similar problem by adding playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

Upvotes: 3

Related Questions