vladasha vladasha - 6 months ago 88
iOS Question

Dismiss video when it is finished playing Swift

I am using the MPMoviePlayerController to play a video. I want to dismiss the video when it has finished playing. Here is my code:

import UIKit
import MediaPlayer

class programViewController: UIViewController {

var moviePlayer : MPMoviePlayerController?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
playVideo("video", type: "mov")
}

func playVideo(navn:String, type:String) {
let path = NSBundle.mainBundle().pathForResource(navn, ofType: type)
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
player.prepareToPlay()
player.scalingMode = .AspectFit
player.shouldAutoplay = true
player.fullscreen = false
self.view.addSubview(player.view)
}
}
}

Answer

It is possible just to delete the view from subviews, and because the last made subview is the movie player view you can delete it just by using this code:

self.view.subviews[self.view.subviews.count-1].removeFromSuperview()