在iOS中通过从ALAsset中检索的url播放video

我对Obj-C和块的想法还是比较新的。 我阅读这篇文章,关于显示从ALAssets中检索的URL的图像 : 显示从iPhone中的ALAsset检索到的URL的图像

除了能够使用ALAsset框架查看UITableView中的图片文件ALAsset ,我还希望能够播放存储在图片文件夹中的video。 video资源就像一张图片一样,所以我希望能够在桌面上点击这个video列表并让它播放。 (换句话说,我想用Assets Library Framework播放video)

这可以用MPMoviePlayerController来完成吗?

从这个post上面我收集我需要在这个函数内的代码来获取一个video文件的资产URL:

 ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { }; 

如果这是正确的,我需要将什么代码放入该函数,以便我可以通过MPMoviePlayerController成功播放video?

仅供参考,我在UITableView显示资产的代码在这里:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = @"id"; UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } [[cell imageView] setImage:[UIImage imageWithCGImage:[[assets objectAtIndex:indexPath.row] thumbnail]]]; [[cell textLabel] setText:[NSString stringWithFormat:@"Item %d", indexPath.row+1]]; return cell; } 

这是我的第一篇文章,所以我很抱歉,如果我发布错误。 谢谢你的帮助

更容易

 MPMoviePlayerViewController *moviePlayerVC =[[MPMoviePlayerViewController alloc] initWithContentURL:[[asset defaultRepresentation] url]]; 

好吧,我发现可以使用这个块来吐出一个资产库地址的日志:

 void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { if(result != nil) { NSLog(@"See Asset: %@", result); [assets addObject:result]; } }; 

这会吐出类似的东西

 "See Asset: ALAsset - Type:Video, URLs:{ "com.apple.m4v-video" = "assets-library://asset/asset.m4v?id=100&ext=m4v"; " 

所以我可以发送这个资产库地址到MPMoviePlayerController ,它将工作!

像这样:

 NSString *urlAddress = @"assets-library://asset/asset.m4v?id=100&ext=m4v"; NSURL *theURL = [NSURL URLWithString:urlAddress]; mp = [[MPMoviePlayerController alloc] initWithContentURL:theURL]; etc... 

现在我只是想弄清楚如何从ALAsset对象dynamic获取URLstring,这不应该太难以弄清楚..希望

您可以使用以下方式获取asseturl

 [result defaultRepresentation] url]; 

@ go2答案是正确的,但MoviePlayerController不显示任何东西。

所以你还需要添加moviePlayerController.movieSourceType=MPMovieSourceTypeFile; 看下面的代码来玩这个。

 MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [moviePlayerController.view setFrame:self.view.frame]; moviePlayerController.movieSourceType=MPMovieSourceTypeFile; moviePlayerController.fullscreen = YES; [moviePlayerController play];