Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add videos to a storyboard page (Xcode 6)

I was woking on a small app project, and I made a video to put into the storyboard. My only problem, however, is that I don't know how to add the video to the storyboard. The ImageView doesn't accept the filetype (.mov). Online, I have only found a tutorial for Xcode 4, and nothing else. I need the video and not a GIF file because I don't want the GIF to loop forever. I'm using Swift.

Thanks!

like image 208
user113085 Avatar asked Nov 20 '25 00:11

user113085


1 Answers

I would recommend you to add the MediaPlayer.framework under the build phase option

Build phase -> link binary with library -> use the add button and type mediaplayer and add the MediaPlayer.framework. Once done add the following code:

import UIKit
import MediaPlayer

class ViewController: UIViewController {
    var moviePlayer: MPMoviePlayerController!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let path = NSBundle.mainBundle().pathForResource("sample", ofType:"mp4")
        let url = NSURL.fileURLWithPath(path!)
        self.moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = self.moviePlayer {
            player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
            player.view.sizeToFit()
            player.scalingMode = MPMovieScalingMode.Fill
            player.fullscreen = true
            player.controlStyle = MPMovieControlStyle.None
            player.movieSourceType = MPMovieSourceType.File
            player.repeatMode = MPMovieRepeatMode.One
            player.play()
            self.view.addSubview(player.view)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
like image 129
sharon devasia Avatar answered Nov 22 '25 14:11

sharon devasia