-1

My movie file starts no problem. The done button does not dismiss the video content. No idea why? Also, Fast Forward and Rewind buttons just cause a black screen. I don't think I am using the notification functions correctly?

import Foundation
import UIKit
import MediaPlayer

class VideoViewController: UIViewController {

var moviePlayer:MPMoviePlayerController!

@IBAction func videoLaunch(sender: AnyObject) {
    playVideo()
}
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("MyVideo", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
moviePlayer?.controlStyle = MPMovieControlStyle.Fullscreen
player.prepareToPlay()
self.view.addSubview(player.view)

}
}


override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "moviePlayBackDidFinish:",
        name: MPMoviePlayerPlaybackDidFinishNotification,
        object: moviePlayer)


    func moviePlayBackDidFinish(notification: NSNotification){
        self.view.removeFromSuperview()
    }



    }
}
Ben Thomas
  • 105
  • 2
  • 12

2 Answers2

1

You are adding player view as subview. You should remove it (removeFromSuperview) after done button pressed. Use notifications to listen for playback finish:

NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "moviePlayBackDidFinish:",
name: MPMoviePlayerPlaybackDidFinishNotification,
object: moviePlayer)

and moviePlayBackDidFinish:

func moviePlayBackDidFinish(notification: NSNotification){
  // remove from superview
}
f3n1kc
  • 2,087
  • 18
  • 18
1

You should remove moviePlayer from your superview like this:

   func moviePlayBackDidFinish(notification: NSNotification){
        let moviePlayer:MPMoviePlayerController = notif.object as! MPMoviePlayerController

    moviePlayer.view.removeFromSuperview()
}

Because in your case you remove self.view

Roman Barzyczak
  • 3,785
  • 1
  • 30
  • 44