Search code examples
iosswiftcocoa-touchmpmovieplayercontroller

Play Video File from iOS Device


I play video on my iOS application.

func playVideo() {
        if let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4"),
            url = NSURL(fileURLWithPath: path),
            moviePlayer = MPMoviePlayerController(contentURL: url) {
                self.moviePlayer = moviePlayer
                moviePlayer.view.frame = self.view.bounds
                moviePlayer.prepareToPlay()
                moviePlayer.controlStyle = MPMovieControlStyle.None
                moviePlayer.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)
                moviePlayer.scalingMode = .AspectFill
                self.view.addSubview(moviePlayer.view)

if (oldMoviePlayer != nil){
                    println("Animation Run")
                    UIView.transitionFromView(oldMoviePlayer!.view, toView: moviePlayer.view, duration: 1.0, options: transitionOptions, completion: nil)
                }
                oldMoviePlayer = moviePlayer

        } else {
            debugPrintln("Ops, something wrong when playing video.m4v")
        }
    }

Now video.mp4 file is saved in my project.

How can I play videos from my device and not from project?

Can I get path and name of video from device?


Solution

  • Ok given your scenario the following documentation should help you - It's written in objective c but you should be able to work out the swift counterpart:

    https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW43

    https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW43

    Please mark as answer when you understand it all :)