<Graphic images, animations, multimedia> Reading Notes-recording and editing videos

Source: Internet
Author: User

Use uiimagepickercontroller for recording

# Import "viewcontroller. H "# import <mobilecoreservices/mobilecoreservices. h> # import <quartzcore/quartzcore. h> @ interface viewcontroller () <uiimagepickercontrollerdelegate, response>-(ibaction) videorecod :( ID) sender; @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib .} -(void) didreceivem Emorywarning {[Super didreceivememorywarning]; // dispose of any resources that can be recreated .} -(ibaction) videorecod :( ID) sender {If ([uiimagepickercontroller issourcetypeavailable: available]) {uiimagepickercontroller * imagepickercontroller = [[uiimagepickercontroller alloc] init]; imagepickercontroller. delegate = self; imagepickercontroller. sourcetype = uiimagep Ickercontrollersourcetypecamera; imagepickercontroller. mediatypes = [[nsarray alloc] initwithobjects :( nsstring *) kuttypemovie, nil]; // sets the recording quality to imagepickercontroller. videoquality = uiimagepickercontrollerqualitytypehigh; // only 30 seconds of imagepickercontroller can be recorded. videomaximumduration = 30366f; [self presentviewcontroller: imagepickercontroller animated: Yes completion: Nil];} else {nslog (@ "camera unavailable. ") ;}}-(Void) Authorization: (uiimagepickercontroller *) picker {[self dismissviewcontrolleranimated: Yes completion: Nil];}-(void) imagepickercontroller: (uiimagepickercontroller *) picker didfinishpickingmediawithinfo: (nsdictionary *) info {nsurl * url = [info objectforkey: uiimagepickercontrollermediaurl]; nsstring * tempfilepath = [URL path]; If (partial Avedphotosalbum (tempfilepath) {// video: didfinishsavingwitherror: contextinfo: the callback must be saved as this type (tempfilepath, self, @ selector (Video: didfinishsavingwitherror: contextinfo :), (_ bridge void *) (tempfilepath);} [self dismissviewcontrolleranimated: Yes completion: Nil];}-(void) Video :( nsstring *) videopath didfinishsavingwitherror :( nserror *) error contextinfo :( nsstring *) c Ontextinfo {nsstring * Title; nsstring * message; If (! Error) {Title = @ "video save"; message = @ "the video has been saved to the camera film of the device";} else {Title = @ "video failed "; message = [error description];} uialertview * Alert = [[uialertview alloc] initwithtitle: Title message: Message delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; [alert show];}-(void) navigationcontroller :( uinavigationcontroller *) navigationcontroller willshowviewcontroller :( uiviewcontroller *) viewcontroller Animated :( bool) animated {nslog (@ "selector will be displayed. ");}-(Void) navigationcontroller :( uinavigationcontroller *) navigationcontroller didshowviewcontroller :( uiviewcontroller *) viewcontroller animated :( bool) animated {nslog (@" the selector ends. ");}

Use avfoundation


Avcapturesession: capture sessions to capture data from the camera and microphone. The avcapturesession object must be used to coordinate input and output data.

Avcapturedevice: capture device, which indicates entering a device, such as a camera and a microphone.

Avcapturedeviceinput: capture an input data source of a session

Avcaptureoutput captures an output target of a session, such as the output video file and static image.

Avcapturemoviefileoutput is a subclass of avcaptureoutput. It can be used to output captured data to a QuickTime video file (mov)

Avcapturevideopreviewlayer is a subclass of calayer that can be used to display recorded videos.

Avcaptureconnection: capture a connection. It is a connection between input and output in a capture session.


# Import "viewcontroller. H "# import <avfoundation/avfoundation. h> # import <assetslibrary/assetslibrary. h> @ interface viewcontroller () <strong> {bool isrecording;} @ property (weak, nonatomic) iboutlet uilabel * label; @ property (weak, nonatomic) iboutlet uibutton * button; @ property (strong, nonatomic) avcapturesession * session; @ property (strong, nonatomic) avcapturemoviefileou Tput * output;-(ibaction) recordpressed :( ID) sender; @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; self. session = [[avcapturesession alloc] init]; self. session. sessionpreset = response; avcapturedevice * cameradevice = [avcapturedevice failed: avmediatypevideo]; nserror * error = nil; avcapturedeviceinput * camera = [avcapturedevicdevicp Ut deviceinputwithdevice: cameradevice error: & error]; avcapturedevice * micdevice = [avcapturedevice failed: Failed]; avcapturedeviceinput * MIC = [cannot deviceinputwithdevice: micdevice error: & error]; if (error |! Camera |! MIC) {nslog (@ "input error");} else {// Add the captured audio and video [self. session addinput: Camera]; [self. session addinput: MIC];} self. output = [[avcapturemoviefileoutput alloc] init]; If ([self. session canaddoutput: Self. output]) {[self. session addoutput: Self. output]; // output} avcapturevideopreviewlayer * previewlayer = [avcapturevideopreviewlayer layerwithsession: Self. session]; previewlayer. frame = cgrectmake (0, 0, se Lf. view. frame. size. width, self. view. frame. size. height); [self. view. layer insertsublayer: previewlayer atindex: 0]; [self. session startrunning]; isrecording = no; self. label. TEXT = @ "";}-(void) didreceivememorywarning {[Super success];}-(void) viewwillappear :( bool) animated {[Super viewwillappear: animated]; If (! [Self. session isrunning]) {[self. session startrunning] ;}}- (void) viewwilldisappear :( bool) animated {[Super viewwilldisappear: animated]; If ([self. session isrunning]) {[self. session stoprunning] ;}}- (ibaction) recordpressed :( ID) sender {If (! Isrecording) {[self. button settitle: @ "stop" forstate: uicontrolstatenormal]; self. label. TEXT = @ "recording... "; isrecording = yes; nsurl * fileurl = [self fileurl]; [self. output startrecordingtooutputfileurl: fileurl recordingdelegate: Self];} else {[self. button settitle: @ "Recording" forstate: uicontrolstatenormal]; self. label. TEXT = @ "stop"; [self. output stoprecording]; isrecording = no;}-(nsurl *) fileurl {nsstr Ing * outputpath = [[nsstring alloc] initwithformat: @ "% @", nstemporarydirectory (), @ "movie. mov "]; nsurl * outputurl = [[nsurl alloc] initfileurlwithpath: outputpath]; nsfilemanager * manager = [[nsfilemanager alloc] init]; If ([Manager fileexistsatpath: outputpath]) {[Manager removeitematpath: outputpath error: Nil];} return outputurl;} # pragma mark -- avcapturefileoutputrecordingdelegate delegate protocol implementation-(voi D) captureoutput :( avcapturefileoutput *) captureoutput didfinishrecordingtooutputfileaturl :( nsurl *) outputfileurl fromconnections :( nsarray *) connections error :( nserror *) error {If (error = nil) {alassetslibrary * library = [[alassetslibrary alloc] init]; [library writevideoatpathtosavedphotosalbum: outputfileurl completionblock: ^ (nsurl * asseturl, nserror * error) {If (error) {nslog (@ "write error. ") ;}}] ;}}


Use uivideoeditorcontroller

# Import "viewcontroller. H "@ interface viewcontroller () <uivideoeditorcontrollerdelegate, response>-(ibaction) editbuttonpress :( ID) sender; @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib .} -(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources th At can be recreated .} -(ibaction) editbuttonpress :( ID) sender {nsbundle * bundle = [nsbundle mainbundle]; nsstring * moviepath = [bundle pathforresource: @ "YY" oftype: @ "MP4"]; // determine whether the device supports video editing. If ([uivideoeditorcontroller caneditvideoatpath: moviepath]) {uivideoeditorcontroller * videoeditor = [[uivideoeditorcontroller alloc] init]; videoeditor. delegate = self; videoeditor. videopath = moviepath; [self pres Entviewcontroller: videoeditor animated: Yes completion: NULL];} else {nslog (@ "Cannot edit this video") ;}}-(void) videoeditorcontroller :( uivideoeditorcontroller *) editor didsaveeditedvideotopath :( nsstring *) editedvideopath {[Editor dismissviewcontrolleranimated: Yes completion: NULL]; If (Response (editedvideopath) {response (editedvideopath, self, @ Selector (Video: Audio: contextinfo :), (_ bridge void *) (editedvideopath) ;}- (void) videoeditorcontroller :( uivideoeditorcontroller *) Editor didfailwitherror :( nserror *) error {nslog (@ "video error editing"); nslog (@ "video editor error occurred = % @", error); [Editor dismissviewcontrolleranimated: Yes completion: NULL];} -(void) videoeditorcontrollerdidcancel :( uivideoeditorcontroller *) Editor {n Slog (@ "video edit canceled"); [Editor dismissviewcontrolleranimated: Yes completion: NULL];}-(void) Video :( nsstring *) videopathdidfinishsavingwitherror :( nserror *) error contextinfo :( nsstring *) contextinfo {nsstring * Title; nsstring * message; If (! Error) {Title = @ "video save"; message = @ "the video has been saved to the camera film of the device";} else {Title = @ "video failed "; message = [error description];} uialertview * Alert = [[uialertview alloc] initwithtitle: Title message: Message delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; [alert show];}

My personal comments: in fact, I have done video before, and there are similar articles. I can find better and more technical Code and articles on some foreign websites, but in short, it's a learning record ~

Original book: http://item.jd.com/11522516.html

<Graphic images, animations, multimedia> Reading Notes-recording and editing videos

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.