0

I'm making an iOS app where the user will be able to record a video and as soon as he finish such record, he can send that video to a server. So far so good! But to find the recorded video is being a headache! How can I find the proper URL for the recorded video? During my tests, I have saved a video in the Supporting Files folder and my code is the following

 NSString *url = [[NSBundle mainBundle] pathForResource:@"trailer_iphone" ofType:@".m4v"];

 NSData *data = [NSData dataWithContentsOfFile:url];

So what could I do to replace that URL for the one in the photo gallery?

In the Apple Developer web site is showing that we can use the AVAsset in order to access the photo gallery. so I copied into my code and I'm trying to figure this out!

//Copied from Apple website
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just videos.
    [group setAssetsFilter:[ALAssetsFilter allVideos]];

    // For this example, we're only interested in the first item.
    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0]
                            options:0
                         usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                             // The end of the enumeration is signaled by asset == nil.
                             if (alAsset) {
                                 ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                                 NSURL *url = [representation url];
                                 AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
                                 // Do something interesting with the AV asset.



//My last code
//NSString *url = [[NSBundle mainBundle] pathForResource:@"trailer_iphone" ofType:@".m4v"];

NSData *data = [NSData dataWithContentsOfURL:url];

2 Answers2

0

Use UISaveVideoAtPathToSavedPhotosAlbum Official Documentation

NoilPaw
  • 714
  • 5
  • 11
  • I'm using! The video is being recorded. My problem is, I don't know how to find the URL of this video in the iOS device. if(compatible){ NSLog(@"It is compatible"); UISaveVideoAtPathToSavedPhotosAlbum([videoURL path], self, @selector(video:didFinishSavingWithError:contextInfo:), NULL); NSLog(@"Saved %@",videoURL); } – Adjaxon Araújo Jan 15 '14 at 03:26
  • I don't think you will be able to get a valid URL for this video. If you want to access the video in the library, you could use a UIImagePickerController. – NoilPaw Jan 15 '14 at 03:35
  • I've seen on the Apple dev site that we can use the AVAsset. I'm going to edit my question to show this part of the code. – Adjaxon Araújo Jan 15 '14 at 03:54
0

For recording a video and send this recorded video file to server you can use AVFoundation framework as follow: AVFoundation Video Recording

AVCaptureMovieFileOutput *movieFileOutput; 

AVCaptureMovieFileOutput implements the complete file recording interface declared by AVCaptureFileOutput for writing media data to QuickTime movie files.

for starting a recoding you have to call below code with passing output file path as parameter with delegate.

//Define output file path.
NSString * filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:["recordVideo" stringByAppendingPathExtension:@"mov"]];

 //Start recording 
[movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] recordingDelegate:self];

And after a some time if you want to stop recording then call

//Stop a recording
[movieFileOutput stopRecording];

after a stop recording delegate methode of AVCaptureFileOutputRecordingDelegate is call so, there you can get recorded file path.

#pragma mark - AVCaptureFileOutputRecordingDelegate Methods.
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{

    NSLog(@"Did finish recording, error %@ | path %@ | connections %@", error, [outputFileURL absoluteString], connections);

    //call upload video APi
    [self VideoUploadApi];

}
Nikhlesh Bagdiya
  • 4,316
  • 1
  • 19
  • 29