微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iphone – ALAssetsLibrary获取视频的路径并在以后播放

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

- (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

有谁可以帮我解决这个问题?

解决方法

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐