Shobhit Bhatt
Shobhit Bhatt

Reputation: 143

Video played using AVPlayer disappears from its container view in swift3

I am playing a video using AVPlayer. I use addSubLayer method of CALayer on a UIView on which I want to play the video. Everything works fine but when I send App in background and then again bring App back in foreground the video disappears(Video duration is just 1 second). Until I send App in background, the video(even though it was finished playing) stays on the container view and we can see it. But as soon as I do background and then foreground of App, video/may be the layer disappears.

Would appreciate the help. Thanks.

Upvotes: 2

Views: 1169

Answers (2)

Harshil
Harshil

Reputation: 209

It looks like you want to keep last frame of your video to stay on container view when you bring your app to foreground.

Please set actionAtItemEnd property of AVPlayer object with

AVPlayerActionAtItemEnd.pause option. This will pause the video when the player finishes playing the video and keep the layer on your container view.

Upvotes: 3

Bhavesh Patel
Bhavesh Patel

Reputation: 596

Please set Observer for Application Background/Foreground. When your app came from Background to Foreground then in your Observer method insert below code.

playerLayer = AVPlayerLayer(player: avPlayer)
view.layer.insertSublayer(playerLayer, at: 0)

Upvotes: -1

Related Questions