3

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

Community
  • 1
  • 1
NSDestr0yer
  • 1,419
  • 16
  • 20

4 Answers4

3

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

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
NSDestr0yer
  • 1,419
  • 16
  • 20
3

Even easier

MPMoviePlayerViewController *moviePlayerVC =[[MPMoviePlayerViewController alloc] initWithContentURL:[[asset defaultRepresentation] url]];
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
go2
  • 31
  • 1
  • 5
    This doesn't work for me. The URL contains something like `assets-library://asset/asset.MOV?id=12345&ext=mov`. The MoviePlayerController doesn't show anything. – Bob Vork Jan 16 '13 at 12:44
0

You can get the url of an asset by using:

[result defaultRepresentation] url];
Jack
  • 16,677
  • 8
  • 47
  • 51
Rob
  • 23
  • 5
0

@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];
Dipen Chudasama
  • 3,063
  • 21
  • 42