5

I'm trying to get my app to play a video file that's been downloaded to the documents directory. I know the file is getting downloaded, but I can't seem to get the file to play, here is my code:

-(IBAction)play{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

NSURL *movieURL = [NSURL fileURLWithPath:path];


_player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self.view addSubview:_player.view];

_player.controlStyle = MPMovieControlStyleDefault;
_player.shouldAutoplay = YES;


[_player setFullscreen:YES animated:YES];


[_player play];

}
Almo
  • 15,538
  • 13
  • 67
  • 95
Kevin Fey
  • 51
  • 1
  • 2
  • You have an error in the NSString *path definition you don't need %@/. But you haven't said what's the output or the error if you get one from the console. – Panagiotis Jul 18 '12 at 19:52
  • Are you sure this thing exists in the documents directory and not the application bundle? I can load a video just fine from the bundle. – CodaFi Jul 18 '12 at 20:09
  • 1
    One word of advice: my app got rejected for saving videos to /Documents, as per the new Storage Guidelines (bottom line: it makes backups slow/heavy). **If** you are targetting iOS 5.0.1 or greater, you can flag a subdirectory inside /Documents as 'skip backup'. Otherwise, your only option is to save to /Caches (and devise a way to redownload the video when it gets deleted) – Nicolas Miari Jul 18 '12 at 21:03
  • Thanks for the advice, I will try to setup a subdirectory. – Kevin Fey Jul 18 '12 at 21:38

2 Answers2

12

This looks like some kind of bug, but you have to set your path like that:

 NSString *vidPath = [[NSBundle mainBundle] pathForResource:@"promo" ofType:@"mp4"];
 NSURL *url = [NSURL fileURLWithPath:vidPath isDirectory:NO]; //THIS IS THE KEY TO GET THIS RUN :) 
 [introPlayer setContentURL:url];
Marcin Małysz
  • 733
  • 8
  • 12
0

Issue will be with this line: NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

Change that to NSString *path = [documentsDirectory stringByAppendingPathComponent:@"piggy.m4v"];

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • I'm getting the above output from the console, I just ran the app again and also got this error Error (null) setting playback mode to 'AVAudioSessionModeDefault' – Kevin Fey Jul 18 '12 at 20:04