首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >播放录制的AVCapture格式的内容

播放录制的AVCapture格式的内容
EN

Stack Overflow用户
提问于 2016-04-26 05:53:01
回答 1查看 109关注 0票数 0

我正在尝试播放录制的会议后,它是记录器的完整视图。有点像"snapchat“

我可以在UIView中录制和播放视频播放,但它是通过“播放”、“完成”和“停止”按钮显示的。我不想这样。我希望它看起来像snapchat。

这是我的代码,我在其中找到了Here,但我做了一点修改。:)

代码语言:javascript
复制
import UIKit
import AVFoundation
import AssetsLibrary
import Photos
import MediaPlayer
import AVKit

class Camera: UIViewController, AVCaptureFileOutputRecordingDelegate {


@IBOutlet var cameraView: UIView!
var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice:AVCaptureDevice!
var CamChoser = false

var moviePlayer: MPMoviePlayerController?
@IBOutlet weak var playback: UIView!

@IBOutlet weak var exitCameraModeButton: UIButton!
@IBAction func exitCameraModeButton(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}


var captureSession = AVCaptureSession()

lazy var cameraDevice: AVCaptureDevice? = {
    let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice]
    return devices.filter{$0.position == .Front}.first
}()

lazy var micDevice: AVCaptureDevice? = {
    return AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
}()

var movieOutput = AVCaptureMovieFileOutput()

private var tempFilePath: NSURL = {

    let tempPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("tempMovie").URLByAppendingPathExtension("mp4").absoluteString
    if NSFileManager.defaultManager().fileExistsAtPath(tempPath) {
        do {
            try NSFileManager.defaultManager().removeItemAtPath(tempPath)
        } catch { }
    }
    return NSURL(string: tempPath)!
}()

private var library = ALAssetsLibrary()
//private var library = PHPhotoLibrary()

@IBOutlet weak var switchCameraButton: UIButton!
@IBAction func switchCameraButton(sender: AnyObject) {
    //startSession()
}



override func viewDidLoad() {
    super.viewDidLoad()

    //start session configuration

    captureSession.beginConfiguration()
    captureSession.sessionPreset = AVCaptureSessionPresetHigh

    let devices = AVCaptureDevice.devices()
    startSession()
}

func startSession() {

    // add device inputs (front camera and mic)
    print(CamChoser)

        captureSession.addInput(deviceInputFromDevice(cameraDevice))
        captureSession.addInput(deviceInputFromDevice(micDevice))

        // add output movieFileOutput
        movieOutput.movieFragmentInterval = kCMTimeInvalid
        captureSession.addOutput(movieOutput)

        // start session
        captureSession.commitConfiguration()
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.cameraView.layer.addSublayer(previewLayer!)
        self.cameraView.bringSubviewToFront(self.exitCameraModeButton)
        self.cameraView.bringSubviewToFront(self.switchCameraButton)
        previewLayer?.frame = self.cameraView.layer.frame
        captureSession.startRunning()
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    print("touch")
    // start capture

    movieOutput.startRecordingToOutputFileURL(tempFilePath, recordingDelegate: self)

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    print("release")
    //stop capture
    movieOutput.stopRecording()

    let videoUrl = movieOutput.outputFileURL
    moviePlayer = MPMoviePlayerController(contentURL: videoUrl)
    moviePlayer!.movieSourceType = MPMovieSourceType.Unknown
    moviePlayer!.view.frame = playback.bounds
    moviePlayer!.scalingMode = MPMovieScalingMode.AspectFill
    moviePlayer!.controlStyle = MPMovieControlStyle.Embedded
    moviePlayer!.shouldAutoplay = true

    playback.addSubview((moviePlayer?.view)!)
    //moviePlayer!.prepareToPlay()
    moviePlayer?.setFullscreen(true, animated: true)
    moviePlayer!.play()
    cameraView.bringSubviewToFront(playback)




}

private func deviceInputFromDevice(device: AVCaptureDevice?) -> AVCaptureDeviceInput? {
    guard let validDevice = device else { return nil }
    do {
        return try AVCaptureDeviceInput(device: validDevice)
    } catch let outError {
        print("Device setup error occured \(outError)")
        return nil
    }
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    if (error != nil) {
        print("Unable to save video to the iPhone  \(error.localizedDescription)")
    } else {
        // save video to photo album
        library.writeVideoAtPathToSavedPhotosAlbum(outputFileURL, completionBlock: { (assetURL: NSURL?, error: NSError?) -> Void in
            if (error != nil) {
                print("Unable to save video to the iPhone \(error!.localizedDescription)")
            }
        })

    }

}

}

EN

回答 1

Stack Overflow用户

发布于 2016-04-26 07:08:35

只想让你知道,iOS 9已经弃用了MPMoviePlayerController。问题是你的控件样式设置为embedded,默认情况下,它显示控件按钮。使用MPMovieControleStyle.None删除控件。

有关更多详细信息,请参阅MPMovieControlStyle文档。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36851764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档