ios - Custom camera , video is not playing with Audio in swift -
i new in swift stake overflow. advanced thank's attention. trying build custom camera record video audio. means video play sound when play video. las few days try build custom camera. followed tutorial still missing camera. try per custom camera recording video. maybe not recording audio. don't understand. searching answer, not find appropriate answer this.
here did
import uikit import avfoundation import svprogresshud import mediaplayer import mobilecoreservices import avkit var videourl = [anyobject]() class testviewcontroller: uiviewcontroller { @iboutlet var viewvidioplayer: uiview! @iboutlet weak var myview: uiview! var session: avcapturesession? var userreponsevideodata = nsdata() var userreponsethumbimagedata = nsdata() override func viewdidload() { super.viewdidload() } override func viewdidappear(_ animated: bool) { super.viewdidappear(animated) }
// here create session func createsession() {
var input: avcapturedeviceinput? let moviefileoutput = avcapturemoviefileoutput() var prevlayer: avcapturevideopreviewlayer? prevlayer?.frame.size = myview.frame.size session = avcapturesession() let error: nserror? = nil { input = try avcapturedeviceinput(device: self.camerawithposition(position: .front)!) } catch {return} if error == nil { session?.addinput(input) } else { print("camera input error: \(string(describing: error))") } prevlayer = avcapturevideopreviewlayer(session: session) prevlayer?.frame.size = myview.frame.size prevlayer?.videogravity = avlayervideogravityresizeaspectfill prevlayer?.connection.videoorientation = .portrait myview.layer.addsublayer(prevlayer!) let documentsurl = filemanager.default.urls(for: .documentdirectory, in: .userdomainmask)[0] let filemainurl = nsurl(string: ("\(documentsurl.appendingpathcomponent("temp"))" + ".mp4")) let maxduration: cmtime = cmtimemake(600, 10) moviefileoutput.maxrecordedduration = maxduration moviefileoutput.minfreediskspacelimit = 1024 * 1024 if self.session!.canaddoutput(moviefileoutput) { self.session!.addoutput(moviefileoutput) } session?.startrunning() moviefileoutput.startrecording(tooutputfileurl: filemainurl! url, recordingdelegate: self) } func camerawithposition(position: avcapturedeviceposition) -> avcapturedevice? { let devices = avcapturedevice.devices(withmediatype: avmediatypevideo) device in devices! { if (device anyobject).position == position { return device as? avcapturedevice } } return nil } @ibaction func pressbackbutton(sender: anyobject) { session?.stoprunning() } @ibaction func record(_ sender: any) { createsession() } @ibaction func play(_ sender: any) { self.videoplay() } func videoplay() { let documentsurl = filemanager.default.urls(for: .documentdirectory, in: .userdomainmask).first! { // directory contents urls (including subfolders urls) let directorycontents = try filemanager.default.contentsofdirectory(at: documentsurl, includingpropertiesforkeys: nil, options: []) print(directorycontents) // if want filter directory contents can this: videourl = directorycontents.filter{ $0.pathextension == "mp4" } [anyobject] print("mp3 urls:",videourl) let playercontroller = avplayerviewcontroller() playercontroller.delegate = self as? avplayerviewcontrollerdelegate let movieurl = videourl[0] print(movieurl) let player = avplayer(url: movieurl as! url) playercontroller.player = player self.addchildviewcontroller(playercontroller) self.view.addsubview(playercontroller.view) playercontroller.view.frame = self.view.frame player.play() player.volume = 1.0 player.rate = 1.0 } catch let error nserror { print(error.localizeddescription) } } }
extension testviewcontroller: avcapturefileoutputrecordingdelegate {
@available(ios 4.0, *) private func captureoutput(captureoutput: avcapturefileoutput!, didstartrecordingtooutputfileaturl fileurl: url!, fromconnections connections: [anyobject]!) { print(fileurl) } func capture(_ captureoutput: avcapturefileoutput!, didfinishrecordingtooutputfileat outputfileurl: url!, fromconnections connections: [any]!, error: error!) { let filemainurl = outputfileurl { let asset = avurlasset(url: filemainurl! url, options: nil) //avurlasset(url: filemainurl as! url, options: nil) print(asset) let imggenerator = avassetimagegenerator(asset: asset) imggenerator.appliespreferredtracktransform = true let cgimage = try imggenerator.copycgimage(at: cmtimemake(0, 1), actualtime: nil) let uiimage = uiimage(cgimage: cgimage) userreponsethumbimagedata = try nsdata(contentsof: filemainurl! url) print(userreponsethumbimagedata.length) print(uiimage) // imagedata = uiimagejpegrepresentation(uiimage, 0.1) } catch let error nserror { print(error) return } svprogresshud.show(with: svprogresshudmasktype.clear) let videofilepath = nsurl(fileurlwithpath: nstemporarydirectory()).appendingpathcomponent("mergevideo\(arc4random()%1000)d")!.appendingpathextension("mp4").absolutestring if filemanager.default.fileexists(atpath: videofilepath) { { try filemanager.default.removeitem(atpath: videofilepath) } catch { } } let tempfilemainurl = nsurl(string: videofilepath)! let sourceasset = avurlasset(url: filemainurl! url, options: nil) let assetexport: avassetexportsession = avassetexportsession(asset: sourceasset, presetname: avassetexportpresetmediumquality)! assetexport.outputfiletype = avfiletypequicktimemovie assetexport.outputurl = tempfilemainurl url assetexport.exportasynchronously { () -> void in switch assetexport.status { case avassetexportsessionstatus.completed: dispatchqueue.main.async(execute: { { svprogresshud .dismiss() self.userreponsevideodata = try nsdata(contentsof: tempfilemainurl url, options: nsdata.readingoptions()) print("mb - \(self.userreponsevideodata.length) byte") } catch { svprogresshud .dismiss() print(error) } }) case avassetexportsessionstatus.failed: print("failed \(assetexport.error)") case avassetexportsessionstatus.cancelled: print("cancelled \(assetexport.error)") default: print("complete") svprogresshud .dismiss() } } }
}
there have done. don't understand missing code. why audio not playing video or why not recoding audio video.
use cocopods project. makes job quiet easy. has instructions on , contains demo project test works intended to.
wiki
Comments
Post a Comment