Swift 如何直接播放保存在文档中的本地视频文件?

Swift 如何直接播放保存在文档中的本地视频文件?,swift,Swift,如何播放本地视频 我将文件下载到本地文档,并在表视图中显示列表文件,但我不知道如何播放它 任何人都有想法这是使用MPMoviePlayerServiceController的代码 NSURL *videoURL = [[NSURL alloc]init]; videoURL = [[NSBundle mainBundle] URLForResource:@"SampleVideo" withExtension:@"mp4"]; MPMoviePlayerViewController *media

如何播放本地视频 我将文件下载到本地文档,并在表视图中显示列表文件,但我不知道如何播放它
任何人都有想法

这是使用MPMoviePlayerServiceController的代码

NSURL *videoURL = [[NSURL alloc]init];
videoURL = [[NSBundle mainBundle] URLForResource:@"SampleVideo" withExtension:@"mp4"];
MPMoviePlayerViewController *mediaplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
mediaplayer.view.frame = CGRectMake(0, 150, self.view.frame.size.width, 250);
mediaplayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
[mediaplayer.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
mediaplayer.moviePlayer.shouldAutoplay=YES;
[self.view addSubview:mediaplayer.view];
[mediaplayer.moviePlayer play];
[mediaplayer.moviePlayer setFullscreen:NO animated:YES];
这是swift版本

let path = NSBundle.mainBundle().pathForResource("video", ofType:"m4v")
let moviePlayer = MPMoviePlayerController(contentURL: NSURL(fileURLWithPath: path!))
moviePlayer.view.frame = self.view.bounds
moviePlayer.prepareToPlay()
moviePlayer.scalingMode = .AspectFill
self.view.addSubview(moviePlayer.view)

希望此代码片段对您有用。

以下是使用
mpmovieplayervewcontroller
的代码

NSURL *videoURL = [[NSURL alloc]init];
videoURL = [[NSBundle mainBundle] URLForResource:@"SampleVideo" withExtension:@"mp4"];
MPMoviePlayerViewController *mediaplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
mediaplayer.view.frame = CGRectMake(0, 150, self.view.frame.size.width, 250);
mediaplayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
[mediaplayer.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
mediaplayer.moviePlayer.shouldAutoplay=YES;
[self.view addSubview:mediaplayer.view];
[mediaplayer.moviePlayer play];
[mediaplayer.moviePlayer setFullscreen:NO animated:YES];
let fileManager = NSFileManager.defaultManager()

let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)

if let documentDirectory:NSURL = urls.first { // No use of as? NSURL because let urls returns array of NSURL
    // This is where the your video file should be in the documents directory
    let yourFinalVideoURL = documentDirectory.URLByAppendingPathComponent("YOURVIDEO.mp4/mov")

    if yourFinalVideoURL.checkResourceIsReachableAndReturnError(nil) {
        // The file already exists, so just return the URL
        let player = AVPlayer(URL: NSURL(fileURLWithPath: path))
        let playerController = AVPlayerViewController()            
        playerController.player = player
        self.presentViewController(playerController, animated: true) {
            player.play()
        }
    }
}
这是swift版本

let path = NSBundle.mainBundle().pathForResource("video", ofType:"m4v")
let moviePlayer = MPMoviePlayerController(contentURL: NSURL(fileURLWithPath: path!))
moviePlayer.view.frame = self.view.bounds
moviePlayer.prepareToPlay()
moviePlayer.scalingMode = .AspectFill
self.view.addSubview(moviePlayer.view)
希望这段代码对您有用

let fileManager = NSFileManager.defaultManager()

let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)

if let documentDirectory:NSURL = urls.first { // No use of as? NSURL because let urls returns array of NSURL
    // This is where the your video file should be in the documents directory
    let yourFinalVideoURL = documentDirectory.URLByAppendingPathComponent("YOURVIDEO.mp4/mov")

    if yourFinalVideoURL.checkResourceIsReachableAndReturnError(nil) {
        // The file already exists, so just return the URL
        let player = AVPlayer(URL: NSURL(fileURLWithPath: path))
        let playerController = AVPlayerViewController()            
        playerController.player = player
        self.presentViewController(playerController, animated: true) {
            player.play()
        }
    }
}
希望能有帮助


希望对您有所帮助。

谢谢您的重播,因为视频路径不正确,无法播放[file:///private/var/mobile/Containers/Data/Application/75F69979-EAAB-4621-B942-057CFE2F4D81/Documents/Big_Buck_Bunny_Trailer.m4v]@阿拉伯福诺啊哈。。我以前使用NSBundle播放视频的错误代码。。我刚刚更新了我的答案。希望现在它能解决您的问题。感谢您的重播,它不起作用,因为视频的路径在[file:///private/var/mobile/Containers/Data/Application/75F69979-EAAB-4621-B942-057CFE2F4D81/Documents/Big_Buck_Bunny_Trailer.m4v]@阿拉伯福诺啊哈。。我以前使用NSBundle播放视频的错误代码。。我刚刚更新了我的答案。希望现在它能解决你的问题。