我有一个别人写的项目。该项目将视频文件保存在此路径中,file://localhost/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4
我想用 MPMoviePlayerController 播放文件,如何访问文件?
我有一个别人写的项目。该项目将视频文件保存在此路径中,file://localhost/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4
我想用 MPMoviePlayerController 播放文件,如何访问文件?
试试这个 :-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"output.mp4"];
在这里,fullPath
您将拥有上面提到的路径。
有关如何播放视频的更多详细信息,请参阅本教程
希望这可以帮助你..
试试这个代码伙伴,
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *videoFilePath = [documentDirectory stringByAppendingPathComponent:@"output.mp4"];
NSURL *videoURL = [NSURL URLWithString:videoFilePath];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
首先加载路径和文件 URL:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *localFilePath = [photoDirectoryPath stringByAppendingPathComponent:@"output.mp4"];
然后使用 URL 加载 MPMoviePlayerController
- (void)initMoviePlayer
{
didExit = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:mMoviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
NSLog(@"initMoviePlayer: %@ ",[self getMovieURL]);
mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]];
mMoviePlayer.shouldAutoplay = YES;
mMoviePlayer.view.hidden = YES;
[mMoviePlayer setControlStyle:MPMovieControlStyleNone];
mMoviePlayer.view.frame = [self.view bounds];
mMoviePlayer.scalingMode = MPMovieScalingModeNone;
[[self view] addSubview:[mMoviePlayer view]];
[mMoviePlayer play];
}
- (void) moviePreloadDidFinish:(NSNotification*)notification {
// start playing the movie
mMoviePlayer.view.hidden = NO;
animateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(playMovie:)
userInfo:nil
repeats:NO];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
}
如果您将上述 URL 存储在某处
NSString *fullURL = @"/var/mobile/Applications/CAC8F2CB-1C7D-4805-BF1A-42B63B258E95/Documents/output.mp4"
NSURL *movieURL = [NSURL fileURLWithPath: isDirectory:YES]; //If YES not works use NO
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
希望有帮助