Control playing video

Hi I'm just taking my first few steps in swift. I found some source code to play videos, which works ok. I'm now trying to add a button to skip forward 120 seconds. It builds ok but when I click on the button nothing happens. I'm assuming its because the "moviePlayer.initialPlaybackTime = 120" part of the code doesnt relate to the part thats plaiying the video in the override func viewDidLoad() ???


import UIKit
import MediaPlayer

class ViewController: UIViewController {

var moviePlayer : MPMoviePlayerController!

@IBAction func SkipButton(sender: AnyObject) {
  moviePlayer.initialPlaybackTime = 120
}

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

  let path = NSBundle.mainBundle().pathForResource("testvideo", ofType:"mp4")
  let url = NSURL.fileURLWithPath(path!)
  moviePlayer = MPMoviePlayerController(contentURL: url)
  if let player = self.moviePlayer {
      player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 170)
      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.
}



}

You set initialPlaybackTIme in the SkipButton method.

`initialPlaybackTime` is the initial time of playback, which means the movie player starts playing at 120 sec in the video, and has no effect when the player has already started playing.


Try this:

    moviePlayer.currentPlaybackTime += 120

Thats it perfect. Thank you so much!

Hi


Just read that iOS9 does away with MPMoviePlayerController. How would I go about doing the above with the AVPlayerViewController. I've go the video playing using the following:

import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let path = NSBundle.mainBundle().pathForResource("test", ofType:"mov")
        let url = NSURL.fileURLWithPath(path!)
        let player = AVPlayer(URL: url)
        let playerController = AVPlayerViewController()
       
        playerController.player = player
        self.addChildViewController(playerController)
        self.view.addSubview(playerController.view)
        playerController.view.frame = self.view.frame
       
        player.play()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
}


But as for skipping to 120 seconds after clicking a button I havent got a clue ? Any ideas?


Thanks Mark.

An AVPlayer instance has the following methods that would be useful:


func currentTime() -> CMTime

func seekToTime(time: CMTime)


If you command-double-click on 'AVPlayer' in your source, it will bring up the generated Swift interface from the header file for AVPlayer, which will list the available methods with basic documentation.


(And command-double-clicking on CMTime in the AVPlayer header or in your code will likewise bring up the generated Swift interface for CMTime...)

Control playing video
 
 
Q