如何通过使用ELCImagePickerController选择视频来获取可播放的视频网址

时间:2014-02-19 07:09:09

标签: ios iphone objective-c elcimagepickercontroller

现在,我正在使用 ELCImagePickerController 选择多个视频但是在选择之后它会给ALAssetTypeVideo而不是public.movi​​e“类型网址,所以我无法用{{1}播放}。 请帮帮我。

我正在尝试使用以下代码:

AVPlayer

1 个答案:

答案 0 :(得分:0)

我通过先将视频保存在临时文件中然后使用存储在临时文件中的视频网址解决了我的问题,现在视频正常播放

+(NSString *) videoAssetURLToTempFile:(NSURL*)url
{
    NSString * surl = [url absoluteString];
    NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4];
    NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate];
    NSString *str = [NSString stringWithFormat:@"%f",ti];
    NSArray *Array = [str componentsSeparatedByString:@"."];
    NSString *fileString =[[NSString alloc]initWithString:[Array objectAtIndex:0]];
    NSLog(@"FileName string is ::::--%@",fileString);


    NSString * filename = [NSString stringWithFormat: @"%@.%@",fileString,ext];
    NSLog(@"FileName is ::::--%@",filename);


    NSString * tmpfile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {

        ALAssetRepresentation * rep = [myasset defaultRepresentation];
        NSUInteger size = [rep size];
        const int bufferSize = 8192;


        //NSLog(@"Writing to %@",tmpfile);
        FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+");
        if (f == NULL) {
            NSLog(@"Can not create tmp file.");
            return;
        }
        Byte * buffer = (Byte*)malloc(bufferSize);
        int read = 0, offset = 0, written = 0;
        NSError* err;
        if (size != 0) {
            do {
                read = [rep getBytes:buffer
                          fromOffset:offset
                              length:bufferSize
                               error:&err];
                written = fwrite(buffer, sizeof(char), read, f);
                offset += read;
            } while (read != 0);
        }
        fclose(f);
    };

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Can not get asset - %@",[myerror localizedDescription]);
    };

    if(url)
    {
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:url
                       resultBlock:resultblock
                      failureBlock:failureblock];
    }
    return tmpfile;
}

有关详细信息,请参阅Getting video from ALAsset