iosvideonsdataalassetfile-copying

Copy Video from Library to DocumentsDocument directory


I am building a task in ios app where, I want to copy the video from library to local app Documents Directory. I have successfully fetch the ALAssest List from Library in an array. Now when I try to copy any video from ALAssest url to our file I am always getting the following error

" Error Domain=NSCocoaErrorDomain Code=262 "The operation couldn’t be completed. (Cocoa error 262.)" UserInfo=0x157d4a70 {NSURL=assets-library://asset/asset.m4v?id=B01C37C9-8C8C-4963-BAC8-EE0356C079DD&ext=m4v"

I have made a folder in document directory to store video

NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"AppVideo"];
NSError *error=nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])    //Does directory already exist?
{
    if (![[NSFileManager defaultManager] createDirectoryAtPath:filePath
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:&error])
    {
        NSLog(@"Create directory error: %@", error);
    }
    NSLog(@"pathToPatientPhotoFolder filePath=%@",filePath);
}

Below is the code for copying data

NSError *error=nil;

NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"AppVideo"];
path = [path stringByAppendingPathComponent:@"myvideo.mp4"];
NSURL * url=nil;
 url=[[[self.dataContainer objectAtIndex:selected] defaultRepresentation] url];
[[NSFileManager defaultManager] copyItemAtURL:url toURL:[NSURL URLWithString:path] error:&error];

if(error)
    NSLog(@"Writing Error=%@",error.description);
else
    NSLog(@"writing has done without error");
`

I have also tried to writing NSData of ALAssest url to file but when I play this with MPMoviePlayerViewController it show only black screen

 ALAssetRepresentation *rep = [[self.dataContainer objectAtIndex:selected] defaultRepresentation];

Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *FileData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:NO];


[[NSFileManager defaultManager] createFileAtPath:path
                                        contents:FileData
                                      attributes:nil];

but find no any solution.


Solution

  • Finally i have solved my problem for writing ALAsest Data to Directroy path i used

     ALAssetRepresentation *rep = [myAssest defaultRepresentation];
    
    Byte *buffer = (Byte*)malloc((NSUInteger)rep.size);
    NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:(NSUInteger)rep.size error:nil];
    dty = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:NO];
    [dty writeToFile:path atomically:YES];
    

    and to play this file

    NSString *path;
    NSArray *paths =    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"AppVideo"];
    path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",MyString]];
    
    self.videoController = [[MPMoviePlayerController alloc] init];
    [self.videoController setContentURL:self.videoURL];
    [self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.videoController.view];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(videoPlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.videoController];
    [self.videoController play];