播放从iOS中ALAsset检索到的URL中的视频 [英] play video from URL retrieved from ALAsset in iOS
问题描述
我仍然是Obj-C的新手和块的想法。我读过这篇关于从ALAssets中检索到的网址显示图片的帖子:显示从iPhone中的ALAsset检索到的URL中的图像
除了能够在 UITableView中查看图片文件
使用 ALAsset
框架,我希望能够播放存储在图片文件夹中的视频。视频资产就像图片一样显示,所以我希望能够在表格中点击该视频列表并让它播放。 (换句话说,我想使用资产库框架播放视频)
可以使用 MPMoviePlayerController完成此操作
?
从我收集的上述帖子中我需要此函数内的代码来获取视频文件的资产URL:
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset * myasset)
{
};
如果这是正确的,我需要在该函数中放入什么代码才能成功播放视频通过 MPMoviePlayerController
?
仅供参考,我的代码用于以 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]];
返回单元格;
}
这是我的第一篇帖子,所以如果我发错了,我很抱歉。谢谢你的帮助
好的,我发现可以使用这个块来吐出资产库地址的日志: / p>
void(^ assetEnumerator)(struct ALAsset *,NSUInteger,BOOL *)= ^(ALAsset * result,NSUInteger index,BOOL *停止)
{
if(result!= nil){
NSLog(@See Asset:%@,result);
[assets addObject:result];
}
};
会吐出类似
<$ p的东西$ p>
请参阅资产:ALAsset - 类型:视频,网址:{
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];
等...
现在我只想弄清楚如何获取该URL字符串动态的ALAsset对象,这不应该太难理解..希望
I'm still fairly new to Obj-C and the idea of blocks. I read this post about displaying images from url retrieved from ALAssets: display image from URL retrieved from ALAsset in iPhone
Besides being able to view picture files in a UITableView
using the ALAsset
framework, I want to be able to play a video that is stored in the pictures folder as well. The video asset shows up just as a picture would, so I would like to be able to tap that video listing in the table and have it play. (In other words, I want to play a video using the Assets Library Framework)
Can this be done with MPMoviePlayerController
?
From that post above I'm gathering I need code inside this function to get an asset URL for a video file:
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
};
If this is right, what code would I need to put inside that function so that I could successfully play the video via MPMoviePlayerController
?
Just for reference, my code for displaying the assets in a UITableView
is here:
- (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;
}
This is my first post so I'm sorry if I post it wrong. Thanks for your help
Okay I found out one can use this block to spit out a log of the asset library address:
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result != nil) {
NSLog(@"See Asset: %@", result);
[assets addObject:result];
}
};
which will spit out something like
"See Asset: ALAsset - Type:Video, URLs:{
"com.apple.m4v-video" = "assets-library://asset/asset.m4v?id=100&ext=m4v"; "
So I can send this asset library address to MPMoviePlayerController
and it will work!
Like so:
NSString *urlAddress = @"assets-library://asset/asset.m4v?id=100&ext=m4v";
NSURL *theURL = [NSURL URLWithString:urlAddress];
mp = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
etc...
now I just gotta figure out how to get that URL string out of the ALAsset object dynamically, which shouldn't be too hard to figure out.. hopefully
这篇关于播放从iOS中ALAsset检索到的URL中的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!