简体   繁体   中英

Loading Video from NSURL

I am saving a video to a specific path in my application.

IE:

My Video Path is /private/var/mobile/Applications/57DBBE40-088E-48CD-AED7-9BDB8FF1E039/tmp/video_66C2B8C6-012C-4608-BA8C-97C7ABA0D721.mp4

I am then trying to play that video using MPMoviePlayerController in a custom frame.

videoPath = [stand stringForKey:@"videoPathKey"];
NSLog(@"My Video Path is %@", videoPath);
  MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:videoPath]];
player.view.frame = CGRectMake(0, 44, 320, 272);
[self.view addSubview:player.view];
[self.view bringSubviewToFront:player.view];
[player play];

This does not seem to work. The video never loads. Any idea why?

And yes, I am getting the proper videoPath.

NSLOG shows this: URL is file:///private/var/mobile/Applications/57DBBE40-088E-48CD-AED7-9BDB8FF1E039/tmp/video_37794C4E-A3D0-4AC0-9964-A9DEBB61C3E2.mp4

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"Your VideoName" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlStr];
mpMoviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
mpMoviePlayerController.view.frame = CGRectMake(0, 0, self.playView.frame.size.width, self.playView.frame.size.height);
[self.playView addSubview:mpMoviePlayerController.view];
[mpMoviePlayerController play];

try this !

This is how I have done it in the past

- (MPMoviePlayerController *) loadFile:(NSString *) fileName intoView:(UIView*) videoView
{
   NSString *thePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];
   NSURL *theurl = [NSURL fileURLWithPath:thePath];
   MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
   [moviePlayer.view setFrame:videoView.bounds];
   [videoView addSubview:moviePlayer.view];
   [moviePlayer setControlStyle:MPMovieControlStyleNone];
   [moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
   moviePlayer.shouldAutoplay = NO;
   return moviePlayer;
}

You should change the URL in order to put the scheme:

NSURL *movieURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://localhost%@",url]];

Your video path would be: file://localhost/private/var/mobile/Applications/57DBBE40-088E-48CD-AED7-9BDB8FF1E039/tmp/video_66C2B8C6-012C-4608-BA8C-97C7ABA0D721.mp4

I hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM