录制视频和播放视频的应用

视频的录制主要有mediaplayer和avfoundation的avplayer来实现

直接在代码的注释中说明

import UIKit
import MediaPlayer
class ViewController: UIViewController {
    //该类封装了mpmovieplayer和uiviewcontroller 是一个视图控制类
    var moviePlayerView:MPMoviePlayerViewController!
    //是核心播放控制类 跟试图控制器没有任何的关系 如果想在视图上显示视频 则需要将mpmovieplayer的view属性添加到要显示的视图层次结构中
    var moviePlayer:MPMoviePlayerController!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(moviePlayer.view)
        //要获取路径的情况下来播放视频
        //设置视频的缩放属性 当视频的尺寸与屏幕的尺寸不一致的时候,通过设置缩放模式可以进行视频在屏幕上的显示形式
        moviePlayer.scalingMode = .AspectFit
        //控制视频的controllerStyle的属性 fullscreen全屏播放 有进度条,快进键等
        moviePlayer.controlStyle = .Fullscreen
        //播放视频
        moviePlayer.play()
        //将视频所在的视图全屏的播放
        moviePlayer.setFullscreen(true, animated: true)
        //判断视频是否播放停止
        moviePlayer.playbackState
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "click:", name: MPMoviePlayerDidEnterFullscreenNotification, object: nil)
    
    }

录制视频的操作

import UIKit
//需要的库
import MobileCoreServices
//代理的方法
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    var imagePicker = UIImagePickerController()
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func videoRecod(sender:UIButton) {
        //判断摄像头是否可用
        if UIImagePickerController.isSourceTypeAvailable(.Camera){
            //创建并进行代理
            imagePicker.delegate = self
            //代表访问的媒体类型属性
            imagePicker.mediaTypes = [kUTTypeMovie as String]
            //录制视频的质量的确定
            imagePicker.videoQuality = .TypeMedium
            //只允许最多录制的时间
            imagePicker.videoMaximumDuration = 30.0
        }
    }
    //用户取消了视频录制操作
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    //用户录制完成视频后
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        //获得媒体的保存路径 是一个临时的路径 是从info参数中取出来的
        let url = info[UIImagePickerControllerMediaURL] as! NSURL
        var tempFilePath = url.path
        //测试是否可以将媒体保存到相机胶卷库中
        if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath!){
            //进行保存 第一个参数是要保存的路径 第二个参数是保存完成后回调方法的对象
            //第三个参数是上下文的信息 第四个参数是保存完成后回调的函数
            UISaveVideoAtPathToSavedPhotosAlbum(tempFilePath!, self, "video:didFinishSavingWithError:contextInfo:", nil)
        }
    }
    //定义回调方法
    func video(video:NSString,didFinishSavingWithError error:NSError,contextInfo
        :UnsafeMutablePointer<Void>) {
            var title = ""
            var message = ""
            //保存失败
            if error.description != ""{
                print("save failed")
                message = error.description
            }else {
                title = "视频保存"
                message = "success"
            }
            let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
            self.presentViewController(alert, animated: true, completion: nil)
    }
    func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        print("选择器将要显示")
    }
    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        print("选择器显示结束")
    }
}


你可能感兴趣的:(录制视频和播放视频的应用)