0

現在、 ELCImagePickerControllerを使用して複数のビデオを選択していますが、選択後にALAssetTypeVideopublic.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 に答える