MPMoviePlayerViewController plays a movie from the Documents directory - objective-c

I have a subfolder called "Video" in the "Documents" folder. And there is somevideo.mp4 file. I have the full path to this file, but MPMoviePlayerViewController does not want to play the video from local. Here is the code:

NSString *path = [[self getVideoFolderPath] stringByAppendingPathComponent:@"somevideo.mp4"]; NSURL *fURL = [NSURL fileURLWithPath:path]; MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:fURL]; [self presentMoviePlayerViewControllerAnimated:videoPlayerView]; [videoPlayerView.moviePlayer play]; 

It does not play. A white screen appears and nothing else. This code works with the remote video when I put the URL on the website with the video file, but the local one does not play. How to get the correct path to the video file?

+3
source share
2 answers

Do you use iOS prior to 4.3? If so, you need to do it.

 [videoPlayerView setContentURL: fURL]; 

Here is a snippet of code from which I am playing a video from a document folder. One of the things I would recommend is to avoid some auto-ad objects. Especially in NSURL when you have viewcontrollers. I know this should be safe, but I did not find it 100%

 if ( [[NSFileManager defaultManager] fileExistsAtPath:fullpathVideoFile] == NO ) { NSLog(@"No video file found"); return self; } playbackURL = [[NSURL alloc] initFileURLWithPath:fullpathVideoFile isDirectory:NO]; if ( playbackURL == nil ) { NSLog(@"playbackURL == nil"); return self; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadedMovie:) name:@"MPMoviePlayerLoadStateDidChangeNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerStartedMovie:) name:@"MPMoviePlayerNowPlayingMovieDidChangeNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinishedMovie:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:nil]; [self setContentURL:playbackURL]; 

In my case, I created a subclass of MPMoviePlayerController (as I usually do with all view controllers), so the references to the self will be associated with an instance of the MPMoviePlayerController object

+6
source

It’s strange. The code you supplied seems to be correct. Make sure that the path to the audio you are trying to extract is not broken.

+1
source

Source: https://habr.com/ru/post/1438384/


All Articles