2

I create one application that show file in document folder. I have one file .mov format that I want show it from document folder but when run app and click play button don't show this movie and I see this image : enter image description here

this is my code : (please guide me and tell me my mistake)

- (IBAction)Play:(id)sender
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString *file = [documentPath stringByAppendingPathComponent:@"xcode4-outlet.mov"];
    NSURL *url = [NSURL fileURLWithPath:file];
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
    [self.view addSubview:_moviePlayer.view];
    _moviePlayer.fullscreen = YES;
    _moviePlayer.shouldAutoplay = YES;
    _moviePlayer.allowsAirPlay = YES;
    [_moviePlayer play];
}

also compiler get me this massage :

movie player[9489:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
movie player[9489:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
movie player[9489:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
fred
  • 157
  • 2
  • 11
  • 1
    `NSString *movPath = [[NSBundle mainBundle] pathForResource:@"xcode4-outlet" ofType:@"mov"]; ` or check the movie exit or not `[[NSFileManager defaultManager] fileExistsAtPath:file]` – Buntylm May 15 '13 at 05:12
  • my friend I want display this movie from document folder !!! – fred May 15 '13 at 05:15

3 Answers3

5

You need to check whether a file is there or not before adding movie player also you can add default

-(IBAction)Play:(id)sender
{

    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString *file = [documentPath stringByAppendingPathComponent:@"xcode4-outlet.mov"];
    NSURL *url = [NSURL fileURLWithPath:file];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
    if(fileExists)
    {
        _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
                                                   object:_moviePlayer];
        _moviePlayer.shouldAutoplay=NO;
        [_moviePlayer prepareToPlay];
    }

}

Add your movie after movie is completely loaded

-(void)moviePreloadDidFinish:(NSNotification*)notification
{

   _moviePlayer.controlStyle=MPMovieControlStyleDefault;
   [self.view addSubview:_moviePlayer.view];
   [_moviePlayer play];
   [_moviePlayer setFullscreen:YES animated:YES];

}
Dhara
  • 4,093
  • 2
  • 36
  • 69
  • my friend in document folder is "xcode4-outlet.mov" but BOOL fileExists get me NO value!!! why? – fred May 15 '13 at 05:59
  • its means there is no such file name xcode4-outlet.mov in your document directory. Check from finder whether such file exists or not – Dhara May 15 '13 at 06:05
  • I nslog file and get this address !!! : Users/fred/Library/Application Support/iPhone Simulator/6.0/Applications/21F0D83A-6C1A-47DC-916F-E37769E2973D/Library/Documentation ....... – fred May 15 '13 at 06:16
  • This is not the path of document directory its path for library and above path is check for document directory. – Dhara May 15 '13 at 06:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29968/discussion-between-dhara-and-fred) – Dhara May 15 '13 at 06:18
4

You may not be supplying a valid path to the URL. It looks like your trying to append your resource to the documents path, but that resource may not actually be there. You have to write the file to that path first. You should always double check your path with:

if ([[NSFileManager defaultManager] fileExistsAtPath:yourPath])
{
//path exists
}

Otherwise, just init the movie player with the straight up resource if you have it inside of your app.

Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
  • my friend in top code you write "yourPath" is this filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"sample.mp4"]; ???? – fred May 15 '13 at 05:21
  • Yes by "yourPath" i just mean the actual path that your passing to the URL. So yes, your comment is correct. Does it return yes or no? – Tommy Devoy May 15 '13 at 05:40
  • That means your movie file is not existing at that path. Go to the path manually and check – Anil Varghese May 15 '13 at 05:59
  • Because you have to have previously written the file to the documents folder. Why do you want to use it from the documents directory? Unless it's a user chosen video or something, its not really necessary...You can just init directly with the resource otherwise – Tommy Devoy May 15 '13 at 06:00
1

Your code looks good. Check the file name is correct.. Also give a try like

-(void)playMovie
{
   NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
   NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"xcode4-outlet.mov"];


    NSURL    *fileURL    =   [NSURL fileURLWithPath:filePath];
    MPMoviePlayerController *moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:fileURL]retain];
   [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlaybackComplete:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayerController];


   [self.view addSubview:moviePlayerController.view];

    moviePlayerController.fullscreen = YES;
   [moviePlayerController play];
  } 

  - (void)moviePlaybackComplete:(NSNotification *)notification
  {
   // playback completed 
  }
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110