Haroon Azhar
Haroon Azhar

Reputation: 13

How to play a local video in a background view in ios

I have a UIViewcontroller that contains a UIView and a button widget. Here is an image of the viewcontroller class

Image of my main VC

I'm trying to play a video on the uiview(like in background) and use the button to stop or play the video but the thing is when I press the play button the video comes(redeners) on top the of the button and I can see and unpause it(well it should restart becuase I haven't written the code to handle that)

here is my ibaction function's code associated with that button:

@IBOutlet weak var videoView: UIView!


@IBAction func playButtonPressed(_ sender: UIButton)
{
    let path = URL(fileURLWithPath: Bundle.main.path(forResource: "color spiral", ofType: "mov")!)

    let player = AVPlayer(url: path)
    let newLayer = AVPlayerLayer(player: player)
    newLayer.frame = self.videoView.bounds
    self.videoView.superview?.layer.addSublayer(newLayer)

    newLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    player.play()

}

this is what I get when I press the button:

image of simulator after i press the button

so how do I play the video but not over my button or keep the button showing?

Upvotes: 1

Views: 314

Answers (1)

kenichinisan
kenichinisan

Reputation: 36

Try dragging the [button] in Document Outline (left panel of Interface Builder) outside of "video view" and into the "view".

Upvotes: 2

Related Questions