天天看点

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.
    }
    */

}