Cowboy Tech

iOS视频播放和录制

Play Video

import UIKit
import MediaPlayer

class ViewController: UIViewController {

var pc:MPMoviePlayerViewController!

@IBAction func playVideoBtnClicked(sender: AnyObject) {

    pc = MPMoviePlayerViewController(contentURL: NSBundle.mainBundle().URLForResource("video", withExtension: "mp4"))
    presentViewController(pc, animated: true, completion: nil)
}
}

Record Video

import UIKit
import MobileCoreServices
import MediaPlayer

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate  {

var picker:UIImagePickerController!
var videoUrl:NSURL?
var player:MPMoviePlayerViewController!

@IBAction func playVideoBtnClicked(sender: AnyObject) {

    if let url = videoUrl{
        player = MPMoviePlayerViewController(contentURL: url)
        presentViewController(player, animated: true, completion: nil)
    }
}


@IBAction func recVideoBtnClicked(sender: AnyObject) {

    picker = UIImagePickerController()
    picker.mediaTypes = [kUTTypeMovie as String]
    picker.sourceType = UIImagePickerControllerSourceType.Camera
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video
    picker.delegate = self

    presentViewController(picker, animated: true, completion: nil) 
}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    videoUrl = info[UIImagePickerControllerMediaURL] as? NSURL

    picker.dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    picker.dismissViewControllerAnimated(true, completion: nil)
}
}