在 iOS 中播放从 ALAsset 检索到的 URL 中的视频

发布于 2024-09-26 08:17:49 字数 1405 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

懵少女 2024-10-03 08:17:49

更容易

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

Even easier

MPMoviePlayerViewController *moviePlayerVC =[[MPMoviePlayerViewController alloc] initWithContentURL:[[asset defaultRepresentation] url]];
じее 2024-10-03 08:17:49

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

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 对象中获取该 URL 字符串,这应该不难弄清楚..希望

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

狠疯拽 2024-10-03 08:17:49

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

[result defaultRepresentation] url];

You can get the url of an asset by using:

[result defaultRepresentation] url];
江南烟雨〆相思醉 2024-10-03 08:17:49

@go2 答案是正确的,但 MoviePlayerController 没有显示任何内容。

所以你还需要添加moviePlayerController.movi​​eSourceType=MPMovieSourceTypeFile;
请参阅下面的代码来播放此内容。

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

@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.

MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:self.view.frame];
moviePlayerController.movieSourceType=MPMovieSourceTypeFile;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文