开发者

play video from URL retrieved from ALAsset in iOS

开发者 https://www.devze.com 2023-01-18 16:19 出处:网络
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

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 *)tableV开发者_如何学Goiew 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


Even easier

MPMoviePlayerViewController *moviePlayerVC =[[MPMoviePlayerViewController alloc] initWithContentURL:[[asset defaultRepresentation] 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


You can get the url of an asset by using:

[result defaultRepresentation] url];


@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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号