2016-04-25 5 views
0

Ich versuche, die aufgenommene Sitzung in voller Ansicht nach dem Recorder wiedergegeben. Ein bisschen wie "Snapchat".Wiedergabe aufgezeichneten Inhalt in AVCapture

Ich kann die Videowiedergabe in einer UIView aufzeichnen und wiedergeben, aber es wird mit den Schaltflächen "Wiedergabe", "Fertig" und "Stopp" angezeigt. Ich will das nicht. Ich möchte, dass es wie Snapchat aussieht.

Das ist mein Code, wo ich Here gefunden habe, aber ich habe ein kleines bisschen geändert. :)

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

    } 

} 

}

Antwort

1

Nur damit Sie wissen, hat MPMoviePlayerController für iOS als veraltet 9. Das Problem ist Ihre Kontrolle Stil eingebettet gesetzt wird standardmäßig die, zeigt die Steuertasten. Verwenden Sie MPMovieControleStyle.None, um die Steuerelemente zu entfernen.

Weitere Details finden Sie in der Dokumentation MPMovieControlStyle.

+0

Vielen Dank für die Hilfe jon, aber gibt es jetzt eine alternative Lösung, wenn MPMoviePlayerController für iOS veraltet ist? –

+0

Verwenden Sie stattdessen AVPictureInPictureController. Stelle sicher, dass du meine Antwort auswählst, anstatt sie zu aktualisieren, wenn ich geholfen habe :) –

+0

naja .. es half irgendwie, aber immer noch nicht :) was ich versuche zu machen ist dies zu machen: moviePlayer = MPMoviePlayerController (contentURL: receivedURL) Movieplayer .movieSourceType = MPMovieSourceType.Unknown Movieplayer .view.frame = view.bounds Movieplayer .scalingMode = MPMovieScalingMode.AspectFill Movieplayer .controlStyle = MPMovieControlStyle.None Movieplayer .shouldAutoplay = true !!!! view.addSubview ((moviePlayer? .view)!) moviePlayer? .setFullscreen (true, animiert: true) moviePlayer! .play() aber mit AVPlayerViewcontroller() –