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

时间:2015-12-23 23:16:47

标签: swift

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

2 个答案:

答案 0 :(得分:4)

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()
        }
    }
}

希望它有所帮助。

答案 1 :(得分:0)

这是使用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 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)

这里是swift版本

    app.post("/insert",function(req,res){
        console.log(req.body);
        /* TODO: Now just check that your drive function is correct, SQL is correct and whether what arguements passed to SQL callback is correct */
        myconnection.query('Insert into cate_tbl (cat_id,cat_name,cat_desc,cat_view_count,cat_status) VALUES ("'+uqid+'","'+req.body.cat_name+'","'+req.body.cat_desc+'","'+tt+'","'+status+'")',function(err, results, fields) {
            //if (err) throw err;
            if (err){
                console.log("DB Error"+err); 
                res.send("add cate_tbl failed"+err);
            }else {
               myconnection.query('Insert into cate_item (cat_it_id,cat_it_name,cat_pid,cat_it_count,cat_it_desc,cat_it_status) VALUES ("'+it_id+'","'+req.body.item_name+'","'+uqid+'","'+req.body.tem_count+'","'+req.body.item_desc+'","'+status+'")',function(err, results, fields) {
                    if (err) {
                        console.log("DB Error"+err);
                        res.send("add cate_item failed"+err);
                    }else{
                        res.send("add success");
                    }
                });
            }
        });
    });

希望此代码段对您有用。

相关问题