天天看點

Swiftt之MPMoviePlayerViewController播放器開發

使用MPMoviePlayerViewController開發視訊播放器界面

1、在項目中加入MediaPlayer.framework架構

2、自定義Controller,繼承自MPMoviePlayerViewController

-》重寫supportedInterfaceOrientations方法,進入控制器後預設橫屏顯示

-》系統進入背景後,移除Controller自己的通知處理,自定義自己的消息通知方法

import UIKit
import MediaPlayer

/**
*  自定義視訊播放控制器
*  重寫supportedInterfaceOrientations方法,進入控制器後橫屏顯示
*  系統進入背景後,移除Controller自己的通知處理,自定義自己的消息通知方法
*/
class CFMoviePlayerViewController: MPMoviePlayerViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        // 在目前控制器中移除系統進入背景所發送的消息通知
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationDidEnterBackgroundNotification, object: nil);
        // 自定義當系統進入背景後所發送的消息通知操作
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "musicPause", name: UIApplicationDidEnterBackgroundNotification, object: nil);
        // 自定義當系統從背景進入前台所發送的消息通知操作
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "musicPlayer", name: UIApplicationDidBecomeActiveNotification, object: nil);
    }

    func musicPause() {
        moviePlayer.pause();
    }

    func musicPlayer() {
        moviePlayer.play();
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func supportedInterfaceOrientations() -> Int {
        return UIInterfaceOrientationMask.LandscapeLeft.rawValue.hashValue;
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}