视频播放中的可滑动视频滤镜

时间:2016-10-27 18:24:18

标签: ios swift cocoapods media-player

我正在使用pod SCRecorder,我正在尝试在单独的视图控制器中播放录制的视频。我没有记录视频的问题但是当我初始化videoPlayer视图控制器时,我得到一个空白屏幕。我只是想知道是否有人使用过pod有任何经验,可以提供见解吗?以下是代码:

RecorderViewController:

@IBOutlet weak var previewView: UIView!

@IBOutlet weak var recordButton: UIButton!
let session = SCRecordSession()
let recorder = SCRecorder()
var exportSession = SCAssetExportSession()
var photoOutput: AVCaptureStillImageOutput?
var photo: UIImage?

let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height

override func viewDidLoad() {
    super.viewDidLoad()

    self.recorder.captureSessionPreset = SCRecorderTools.bestCaptureSessionPresetCompatibleWithAllDevices()

    if (!recorder.startRunning()) {
        debugPrint("Recorder error: ", recorder.error)
    }

    recorder.session = session
    recorder.device = AVCaptureDevicePosition.Back
    recorder.videoConfiguration.size = CGSizeMake(screenWidth, screenHeight)
    recorder.delegate = self        
}

func capturePhoto(image: UIImage) {
    var connection = photoOutput!.connectionWithMediaType(AVMediaTypeVideo)
         photoOutput!.captureStillImageAsynchronouslyFromConnection(photoOutput!.connectionWithMediaType(AVMediaTypeVideo)) {
        (imageDataSampleBuffer: CMSampleBuffer!, error: NSError!) in

        if error == nil {
            let data:NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
            let image:UIImage = UIImage( data: data)!

            //add showPhoto call so that functions won't have to be called and can be called from one central place
            // BY 8PM TONIGHT APP SHOULD BE FINISHED!!!!!!!!!!!!!!!
        } else{
            print("Error")
        }
    }
}

func showPhoto(photo: UIImage) {
    self.photo = photo
    self.performSegueWithIdentifier("Photo", sender: self)
}

@IBAction func recordButtonTapped(sender: UIButton) {
    recorder.record()

    if recorder.isRecording{

        print("video is recording")
    }
}

func showVideo() {
    self.performSegueWithIdentifier("Video", sender: self)
}

@IBAction func stopTapped(sender: UIButton) {
    print("video was paused")
    recorder.pause()
    self.showVideo()
}

override func viewWillLayoutSubviews() {

}

override func viewDidLayoutSubviews()
{
    recorder.previewView = previewView  
}

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

的PlayerController:

override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)      
        // Custom initialization
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    deinit {
        var player : SCPlayer?
        self.filterView.removeObserver(self, forKeyPath: "selectedFilter")
        self.filterView = nil
        self.player.pause()
        player = nil 
    }

    let player = SCPlayer()
    let session = SCRecordSession()

    @IBOutlet weak var filterView: SCSwipeableFilterView!

    override func viewWillAppear(animated: Bool) {
        player.setItemByAsset(session.assetRepresentingSegments())

        let playerLayer = AVPlayerLayer(player: player)
         filterView.frame = self.view.bounds
        filterView.layer.addSublayer(playerLayer)
        self.filterView.contentMode = .ScaleAspectFill

        player.play()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if NSProcessInfo.processInfo().activeProcessorCount > 1 {
            self.filterView.contentMode = .ScaleAspectFill
            var emptyFilter = SCFilter.emptyFilter()
            emptyFilter.name = "#nofilter"
            self.filterView.filters = [emptyFilter, SCFilter(CIFilterName:"CIPhotoEffectNoir"), SCFilter(CIFilterName:"CIVignetteEffect"), SCFilter(CIFilterName:"CIPhotoEffect") ]
            self.player.SCImageView = self.filterView
            self.filterView.addObserver(self, forKeyPath: "selectedFilter", options: .New, context: nil)
        }
        else {
            var playerView = SCVideoPlayerView(player: player)
            playerView.playerLayer!.videoGravity = "ResizeAspectFill"
            playerView.frame = self.filterView.frame
            playerView.autoresizingMask = self.filterView.autoresizingMask
            self.filterView.superview!.insertSubview(playerView, aboveSubview: self.filterView)
            self.filterView.removeFromSuperview()
        }

        player.loopEnabled = true   
    }

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

    /*
    // MARK: - Navigation

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

0 个答案:

没有答案