Search code examples
iosobjective-cvideompmovieplayercontroller

Playing video in iOS failing


I just want to load a video file which is in the main bundle, this seems pretty simple but for some reason I keep getting an error of the MPMoviePlayerController, I have the following code.

- (void)viewDidLoad{
[super viewDidLoad];

NSString *path = [[NSBundle mainBundle] pathForResource:@"ipad2" ofType:@"mp4"];

self.myPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
[self.myPlayer prepareToPlay];

self.myPlayer.movieSourceType = MPMovieSourceTypeFile;
[self.myPlayer.view setFrame:self.view.bounds];
[self.view addSubview:self.myPlayer.view];

[self.myPlayer play];

}

I only get a black screen and the following output:

2013-01-09 13:38:15.686 myVideoApp[1789:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-09 13:38:15.690 myVideoApp[1789:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.

I also tried adding these notification for playing but is never sent:

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(playVideo:)
 name:MPMoviePlayerLoadStateDidChangeNotification
 object:self.myPlayer ];

And when I print self.myPlayer.loadState I get 0which is undefined loadState.
These is a simple viewController with any other method, and I have these declaration on the .h file: @property (nonatomic, strong) MPMoviePlayerController *myPlayer;

I`m running on iOS 6, and these happens both in device and simulator


Solution

  • NSString *path = [[NSBundle mainBundle] pathForResource:@"ipad2" ofType:@"mp4"];
    

    Have you checked in the debugger that path is not nil?

    self.myPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
    

    That's the wrong way to create a URL from a file path. Use +[NSURL fileURLWithPath:] instead.