Search code examples
iosswiftmpmovieplayercontroller

How to play 2 video at the same time?


I'am trying to play two video in one screen via MPMoviePlayerController but everytime only one of them plays. It is my playVideo function:

func playVideo() {
    let url = NSURL(string: videoUrls[videoNumber])
    moviePlayer = MPMoviePlayerController(contentURL: url)
    if let player = moviePlayer {
        player.view.frame = CGRect(x: 0, y: 200, width: 200, height: 200)
        player.shouldAutoplay = true
        player.prepareToPlay()
        player.scalingMode = .AspectFill
        player.controlStyle = .None
        player.allowsAirPlay = false
        self.view.addSubview(player.view)
    }

    let backUrl = NSURL(string: videoUrls[videoNumber+1])
    backPlayer = MPMoviePlayerController(contentURL: backUrl)
    if let player = backPlayer {
        player.view.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
        player.shouldAutoplay = true
        player.prepareToPlay()
        player.scalingMode = .AspectFill
        player.controlStyle = .None
        player.allowsAirPlay = false
        self.view.addSubview(player.view)
    }

Solution

  • Refer to the docs:

    Note

    Although you can create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time can play its movie.

    If you want to be able to play two movies at once, check out AVPlayerLayer and AVSynchronizedLayer