gpt4 book ai didi

ios - 在 AVCapture 中播放录制的内容

转载 作者:行者123 更新时间:2023-11-29 01:02:31 25 4
gpt4 key购买 nike

我正在尝试在录制后以完整 View 播放录制的 session 。有点像“snapchat”。

我可以在 UIView 中录制和播放视频播放,但它显示为“播放”、“完成”、“停止”按钮。我不想要那个。我希望它看起来像 snapchat。

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

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)")
}
})

}

}

最佳答案

如您所知,iOS 9 已弃用 MPMoviePlayerController。问题是您的控件样式设置为嵌入,默认情况下显示控件按钮。使用 MPMovieControleStyle.None 删除控件。

参见 MPMovieControlStyle文档以获取更多详细信息。

关于ios - 在 AVCapture 中播放录制的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36851764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com