Play video from URL obtained from ALAsset on iOS

I'm still pretty 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 on iPhone

Besides the ability to view image files in UITableViewusing the structure ALAsset, I want to be able to play video, which is also stored in a folder with pictures. The video ad is displayed in the same way as the image, so I would like to use this list of videos in a table and play it. (In other words, I want to play the video using the structure of the object library)

Can this be done with MPMoviePlayerController?

From this post above I am going to. I need the code inside this function to get the resource URL for the video file:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
    };

If this is correct, what code will I need to put inside this function so that I can successfully play the video through MPMoviePlayerController?

For reference only, my code for displaying assets in UITableViewis 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 incorrectly. Thank you for your help.

+3
source share
4 answers

Well, I found out that this block can be used to highlight the resource library address log:

void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) 
{

   if(result != nil) {
        NSLog(@"See Asset: %@", result);
        [assets addObject:result];

    }
};

which spit out something like

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

, URL ALAsset , . ,

+3

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

You can get url assetusing:

[result defaultRepresentation] url];
0
source

@ go2 the answer is correct, but MoviePlayerController shows nothing.

so you also need to add moviePlayerController.movieSourceType=MPMovieSourceTypeFile; see below code to reproduce this.

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

Source: https://habr.com/ru/post/1768071/


All Articles