Skip to content

Playing Video

changsanjiang edited this page Oct 20, 2019 · 8 revisions

In this page

Create Player

class ViewController: UIViewController {

    let player = SJVideoPlayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(player.view)
        self.player.view.frame = .init(x: 0, y: 0, width: 300, height: 300)
        self.player.view.center = self.view.center
    }
    
}

Create Asset

    if let url = URL.init(string: "http://.../demo.mp4") {
        self.player.urlAsset = SJVideoPlayerURLAsset.init(url: url)
    }
    
// the url also can be a fileURL
//    let url = URL.init(fileURLWithPath: ".../path/demo.mp4")
//    self.player.urlAsset = SJVideoPlayerURLAsset.init(url: url)

Playback Control

// Play. Begins playback of the current asset
    self.player.play()

// Pause. Pauses playback of the current asset
    self.player.pause()

// Stop. Stops playback and clean current asset
    self.player.stop()

// Refresh. Recreate current asset
    self.player.refresh()

// Replay. Seek to the starting position to play
    self.player.replay()

// SeekToTime. Seek to the specified time to play
    let seconds = 10.0
    self.player.seek(toTime: seconds) { (_) in
        // ...
    }
Clone this wiki locally