在 iOS 中播放从 ALAsset 检索到的 URL 中的视频
我对 Obj-C 和块的概念还很陌生。我读了这篇关于显示从 ALAssets 检索到的 url 中的图像的文章: display从 iPhone 中的 ALAsset 检索到的 URL 中的图像
除了能够使用 ALAsset
框架在 UITableView
中查看图片文件之外,我还希望能够播放视频它也存储在图片文件夹中。视频资源的显示方式就像图片一样,因此我希望能够点击表格中的视频列表并播放它。 (换句话说,我想使用资源库框架播放视频)
这可以通过 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]];
return cell;
}
这是我的第一篇文章,所以如果我发布错误,我很抱歉。感谢您的帮助
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更容易
Even easier
好吧,我发现可以使用这个块来输出资产库地址的日志:
它将输出类似的内容
所以我可以将此资产库地址发送到
MPMoviePlayerController
并且它会起作用!像这样:
现在我只需要弄清楚如何动态地从 ALAsset 对象中获取该 URL 字符串,这应该不难弄清楚..希望
Okay I found out one can use this block to spit out a log of the asset library address:
which will spit out something like
So I can send this asset library address to
MPMoviePlayerController
and it will work!Like so:
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
您可以使用以下方法获取
asset
的url
:You can get the
url
of anasset
by using:@go2 答案是正确的,但 MoviePlayerController 没有显示任何内容。
所以你还需要添加moviePlayerController.movieSourceType=MPMovieSourceTypeFile;
请参阅下面的代码来播放此内容。
@go2 answer is correct but The MoviePlayerController doesn't show anything.
so you also need to add
moviePlayerController.movieSourceType=MPMovieSourceTypeFile;
see below code to play this.