Arabfono
Arabfono

Reputation: 21

how to play local video file saved in document directly?

how to play video from local i was download file to local document and i show the list files in table view but i don't know how to play it anyone have idea

Upvotes: 0

Views: 3618

Answers (2)

Nilesh Patel
Nilesh Patel

Reputation: 6394

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

Hope it helps.

Upvotes: 3

Mrugesh Tank
Mrugesh Tank

Reputation: 3560

here is the code using MPMoviePlayerViewController.

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];

and here is the swift version

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)

hope this code snippet will be useful to you.

Upvotes: 0

Related Questions