Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Handle the Controls in MPMoviePlayerController using SWIFT?

I am using MPMoviePlayerController to play videos in my app. Now I need to add some custom buttons like "Next Track" or "Previous Track" and remove some unwanted buttons-

After adding buttons, I have to provide actions for each button.

How can I achieve this in swift?

func Player(url :NSURL){
    moviePlayer = MPMoviePlayerController(contentURL: url)

    self.view.addSubview(moviePlayer.view)
    moviePlayer.fullscreen = true

    moviePlayer.controlStyle = MPMovieControlStyle.Fullscreen
}
like image 911
AruLNadhaN Avatar asked Jul 28 '15 09:07

AruLNadhaN


1 Answers

It is definitely possible to add custom controls for MPMoviePlayerController. Since you want to hide some of them, it would be actually easier for you to recreate all of them so your UI will fit. First, hide default controls:

player.controlStyle = MPMovieControlStyle.None

I would suggest then to subclass MPMoviePlayerController and make your own class, that will create your own controls, like this:

class Player : MPMoviePlayerController {

    // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    // MARK: - Setup

    override func viewDidLoad() {

        // Don't forget to call super
        super.viewDidLoad()

        // Setup your UI
        self.setupCustomControls()
    }


    func setupCustomControls() {

        // Create buttons, labels etc. here
        let button = UIButton(frame: CGRectZero)
        self.view.addSubview(button)
    }
}

Then, instead of using regular MPMoviePlayerController, just use your custom one!

Hope it helps!

like image 156
Jiri Trecak Avatar answered Nov 15 '22 19:11

Jiri Trecak