MPMoviePlayerController does not play from the Documents folder

Desperated. Hello! I am having some problems with MPMoviePlayerController. I did this while working with a video from NSBundle. But that is not what I need. I need to play it from the Documents directory, because this is the place where I store recorded videos whose URLs are stored in CoreData. but let's leave this aside and simplify the code to the minimum necessary. This code actually WORKS if the contentURL that leads to the NSBundle is used. After that, what am I doing to get to the place of documents. What am I doing:

NSURL *contentURL = [[NSBundle mainBundle] URLForResource:@"Oct_08_2012_10_00_51" withExtension:@"mp4"]; // this works NSString* docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString * docaPathFull = [NSString stringWithFormat:@"%@%@", docPath, @"/Oct_08_2012_10_00_51.mp4"]; NSURL * docUrl= [NSURL URLWithString: docaPathFull]; BOOL ex = [[NSFileManager defaultManager] fileExistsAtPath:docaPathFull]; NSLog(@"file exists: %d, path using docPath: %@",ex, docaPathFull); MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:docUrl]; player.shouldAutoplay = YES; player.controlStyle = MPMovieControlStyleEmbedded; [player.view setFrame: self.thumbButton.bounds]; [player prepareToPlay]; [self.view addSubview: player.view]; [player play]; 

What do we have:

 2012-10-08 13:14:43.532 Voto[11968:907] file exists: 1, path using docPath: /var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Documents/Oct_08_2012_10_00_51.mp4 2012-10-08 13:14:43.907 Voto[11968:907] content URL: file://localhost/var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Voto.app/Oct_08_2012_10_00_51.mp4 2012-10-08 13:14:44.265 Voto[11968:907] doc URL: /var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Documents/Oct_08_2012_10_00_51.mp4 2012-10-08 13:14:45.343 Voto[11968:907] [MPAVController] Autoplay: Disabling autoplay for pause 2012-10-08 13:14:45.344 Voto[11968:907] [MPAVController] Autoplay: Disabling autoplay 2012-10-08 13:14:46.518 Voto[11968:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 2012-10-08 13:14:46.540 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay 2012-10-08 13:14:46.554 Voto[11968:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0 2012-10-08 13:14:46.555 Voto[11968:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up. 2012-10-08 13:14:46.557 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay 2012-10-08 13:14:46.567 Voto[11968:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0 2012-10-08 13:14:46.871 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay 

So the file exists ... The questions I looked at:

MPMoviePlayer downloads and plays movies saved in application documents

MPMoviePlayerController does not work with movie in document folder

MPMoviePlayerViewController plays a movie from a document catalog - objective-c

I also checked ut with a reference to the class, nothing concrete about how to play from documents. Settings for my projects: using the latest version of iOS 6, Deployment Goal 5.0 Testing on iOS6 iPhone simulator and iPad with iOS 6. If I forgot to add something, please remind me, I will do it immediately.

Please, help!:)

+4
source share
3 answers

Good thing you are not creating the file url correctly, you should do it like this:

 NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *docaPathFull = [docPath stringByAppendingPathComponent:@"/Oct_08_2012_10_00_51.mp4"]; NSURL *docUrl= [NSURL fileURLWithPath:docaPathFull]; 

You must add directories and a file to the path using the stringByAppendingPathComponent NSString method; Also when creating the file fileURLWithPath: use fileURLWithPath: on NSURL , this willl will create the correct NSURL for the transfer path.

+20
source

The most common mistake everyone makes is to use

 NSURL *fileURL = [NSURL URLWithString:mVidPath]; ^^^^^^^^^^^^^ 

instead

 NSURL *fileURL = [NSURL fileURLWithPath:mVidPath]; ^^^^^^^^^^^^^^^ 
+10
source
 -(IBAction)playVideo { NSURL *vedioURL; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; NSLog(@"files array %@", filePathsArray); NSString *fullpath; for ( NSString *apath in filePathsArray ) { fullpath = [documentsDirectory stringByAppendingPathComponent:apath]; vedioURL =[NSURL fileURLWithPath:fullpath]; } NSLog(@"vurl %@",vedioURL); MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL]; [player.view setFrame: self.view.bounds]; [player.moviePlayer prepareToPlay]; [self.view addSubview:player.view]; player.moviePlayer.controlStyle = MPMovieControlStyleDefault; player.moviePlayer.shouldAutoplay = YES; [player.moviePlayer setFullscreen:YES animated:YES]; [player.moviePlayer play]; [self presentMoviePlayerViewControllerAnimated: player]; } 

Remember to add MediaPlayer.framework and #import <MediaPlayer / mediaplayer.h> in your corresponding code. Good luck !!!

0
source

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


All Articles