Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing video view in the Storyboard

I want to build simple video app thats views a video form youtube links thats users add. I didn't find "VideoView" I mean If image view is for image what is UIView for videos.

like image 522
marrioa Avatar asked Sep 03 '15 06:09

marrioa


3 Answers

There is no object in the original library that performs video viewing function. But you can import the MediaPlayer framework in your project and add it programmatically.

Here is a Swift example

import MediaPlayer

class Step1ViewController: UIViewController {

var moviePlayer: MPMoviePlayerController?

override func viewDidLoad() {
    super.viewDidLoad()

    playVideo()
}

func playVideo() {

    let videoView = UIView(frame: CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.width, self.view.bounds.height))

    let pathToEx1 = NSBundle.mainBundle().pathForResource("myVideoFile", ofType: "mp4")
    let pathURL = NSURL.fileURLWithPath(pathToEx1!)
    moviePlayer = MPMoviePlayerController(contentURL: pathURL)
    if let player = moviePlayer {
        player.view.frame = videoView.bounds
        player.prepareToPlay()
        player.scalingMode = .AspectFill
        videoView.addSubview(player.view)
    }

    self.view.addSubview(videoView)
}

}

As for further customisations and app notifications it has a bunch of in-build possibilities. So check it out.

like image 117
David Robertson Avatar answered Oct 20 '22 14:10

David Robertson


To play video apple has provided MPMovieViewController see this https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html

and this

http://www.brianjcoleman.com/tutorial-play-video-swift/

In youtube Video case we got embedded links are used so for that you will get help from this https://github.com/gilesvangruisen/Swift-YouTube-Player

like image 45
baydi Avatar answered Oct 20 '22 14:10

baydi


import AVFoundation
import AVKit

class ViewController: UIViewController { 
    var player = AVPlayer()
    var playerController = AVPlayerViewController()

    func playVideo() {     
        let videoURL = NSURL(string: videoUrl)
        player = AVPlayer(url: videoURL! as URL)
        let playerController = AVPlayerViewController()
        playerController.player = player
        self.addChildViewController(playerController)

    // Add your view Frame
         playerController.view.frame = self.view.frame

   // Add subview in your view
         self.view.addSubview(playerController.view)

   player.play()
 }

 func stopVideo() {
    player.pause()
 }
}
like image 3
Sazzadhusen Iproliya Avatar answered Oct 20 '22 16:10

Sazzadhusen Iproliya