0

现在,我正在使用ELCImagePickerController选择多个视频,但选择后它给出ALAssetTypeVideo的不是 public.movi​​e" 类型的 url,所以我无法用 . 播放这个AVPlayer。请帮助我。

我正在尝试以下代码:

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];

    for (UIView *v in [_scrollView subviews]) {
        [v removeFromSuperview];
    }

    CGRect workingFrame = _scrollView.frame;
    workingFrame.origin.x = 0;

    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];

    for (NSDictionary *dict in info) {

        NSURL *urlvideo = [dict objectForKey:UIImagePickerControllerReferenceURL];
        NSLog(@"Video URl is::::--%@",urlvideo);
        UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
        [images addObject:image];

        UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
        [imageview setContentMode:UIViewContentModeScaleAspectFit];
        imageview.frame = workingFrame;

        [_scrollView addSubview:imageview];

        workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
    }

    self.chosenImages = images;

    [_scrollView setPagingEnabled:YES];
    [_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}
4

1 回答 1

0

我已经解决了我的问题,首先将视频保存在临时文件中,然后使用存储在临时文件中的视频 url,现在视频可以正常播放

+(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;
}

有关更多详细信息,请参阅从 ALAsset 获取视频

于 2014-03-05T11:20:50.213 回答