ALAssetsLibrary获取视频的路径并稍后播放 [英] ALAssetsLibrary getting the path of a video and play it later

查看:164
本文介绍了ALAssetsLibrary获取视频的路径并稍后播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我可以在tableview中获取视频文件。但我无法获得视频的路径,以便我保存并稍后使用它。

Using below code I am able to get the video files in tableview. But I am unable to get the path to the video so that I would save it and use it later to play.

    - (void)viewDidLoad {

    [super viewDidLoad];
    [activity startAnimating];

    assets = [[NSMutableArray alloc] init];
    library = [[ALAssetsLibrary alloc] init];

    UIImage *viewImage;

    [library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){  
        if (error) {  
            NSLog(@"error");  
        } else {  
            NSLog(@"url %@", assetURL);


        }  
    }];  


    [library enumerateGroupsWithTypes:ALAssetsGroupAll  usingBlock:^(ALAssetsGroup *group, BOOL *stop){

        if (group != NULL) {

            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){


                if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
                    NSLog(@"asset: %@", result);
                    [assets addObject:result];
                }

            }];
        }

        [self.tableview reloadData];
        [self.activity stopAnimating];
        [self.activity setHidden:YES];

    }
           failureBlock:^(NSError *error){

                NSLog(@"failure"); }];

}


// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [assets count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    ALAsset *asset = [assets objectAtIndex:indexPath.row];
    [cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
    [cell.textLabel setText:[NSString stringWithFormat:@"Video %d", indexPath.row+1]];

    return cell;
}

这是我的输出:

2012-07-19 12:37:42.135 mptest[17310:707] asset: ALAsset - Type:Video, URLs:{
    "com.apple.quicktime-movie" = "assets-library://asset/asset.MOV?id=336068EA-C1B1-481C-82DA-F2419561A91A&ext=MOV";
}
2012-07-19 12:37:42.147 mptest[17310:707] asset: ALAsset - Type:Video, URLs:{
    "com.apple.quicktime-movie" = "assets-library://asset/asset.MOV?id=A1CBDDE4-4BC1-48F2-84E0-028D7B7F4879&ext=MOV";
}
2012-07-19 12:37:42.156 mptest[17310:707] asset: ALAsset - Type:Video, URLs:{
    "com.apple.quicktime-movie" = "assets-library://asset/asset.MOV?id=3D76ABC7-515C-42E7-A940-B149C78FBAB6&ext=MOV";
}
2012-07-19 12:37:42.262 mptest[17310:707] error

任何人都可以帮我解决这个问题吗?

Can anyone help me wih this issue?

推荐答案

你无法从中获取实际的文件路径AssetsLibrary因为沙盒。但是,您有多种选项可以访问/播放视频文件。

You can't get the actual file-path from the AssetsLibrary because of sandboxing. However, you have various options to access/play the video file.

1)使用 url ALAssetRepresentation 的方法,并将其传递给MPMoviePlayerController的实例以播放视频。此url以assets-library://开头,并且不是文件系统URL,但是 MPMoviePlayerController 知道如何处理这样的URL。

1) Query the URL of the Asset using the url method of ALAssetRepresentation and pass it to an instance of MPMoviePlayerController to play the video. This url starts with assets-library:// and is not a file-system url, but MPMoviePlayerController knows how to handle such an URL.

2)使用 getBytes:fromOffset获取视频内容:长度:错误: ALAssetsRepresentation 将视频保存到您自己的应用程序沙箱中以播放/编辑/共享它,或者使用 getBytes:fromOffset:length:error:来流式传输视频内容。

2) Get the video contents by using the getBytes:fromOffset:length:error: of ALAssetsRepresentation to save the video to your own app sandbox to play/edit/share it or use getBytes:fromOffset:length:error: to stream the video contents.

这篇关于ALAssetsLibrary获取视频的路径并稍后播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆