13

I need to use the AVAsset object, in order to play it using AVPlayer and AVPlayerLayer. I started using the Photos framework since AssetsLibrary is deprecated. Now I got to the point where I have an array of PHAsset objects and I need to convert them to AVAsset. I tried enumerating through the PHFetchResult and allocation a new AVAsset using the PHAsset's localized description, but it does not seem to show any video when I play it.

    PHAssetCollection *assetColl = [self scaryVideosAlbum];

    PHFetchResult *getVideos = [PHAsset fetchAssetsInAssetCollection:assetColl options:nil];

    [getVideos enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
            NSURL *videoUrl = [NSURL URLWithString:asset.localizedDescription];
            AVAsset *avasset = [AVAsset assetWithURL:videoUrl];
            [tempArr addObject:avasset];
    }];

I assume the localized description is not the absolute url of the video.

I also stumbled upon the PHImageManager and the requestAVAssetForVideo, however, the options parameter when it comes down to video does not have an isSynchrounous property, which is the case with the image options parameter.

        PHVideoRequestOptions *option = [PHVideoRequestOptions new];
        [[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:option resultHandler:^(AVAsset * _Nullable avasset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {

Is there a synchronous way to do this?

Thanks.

Objectif
  • 344
  • 1
  • 3
  • 11

5 Answers5

27

No, there isn't. But you can build a synchronous version:

dispatch_semaphore_t    semaphore = dispatch_semaphore_create(0);

PHVideoRequestOptions *option = [PHVideoRequestOptions new];
__block AVAsset *resultAsset;

[[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:option resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
    resultAsset = avasset;
    dispatch_semaphore_signal(semaphore);
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
// yay, we synchronously have the asset
[self doSomethingWithAsset:resultAsset];

However if you do this on the main thread and requestAVAssetForVideo: takes too long, you risk locking up your UI or even being terminated by the iOS watchdog.

It's probably safer to rework your app to work with the asynchronous callback version. Something like this:

__weak __typeof(self) weakSelf = self;

[[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:option resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf doSomethingWithAsset:avasset];
    });
}];
Rhythmic Fistman
  • 30,464
  • 5
  • 74
  • 138
  • Works like a charm!! But what of the two options do you recommend? I'm using the second one, and also, What is `__weak __typeof(self) weakSelf = self;` ? – CGR Dec 07 '16 at 23:17
  • 1
    The second option is more flexible (it can be called on the main thread without fear) so I would choose that. The `weakSelf` idiom is a way of avoiding retain cycles: http://stackoverflow.com/q/20030873/22147 – Rhythmic Fistman Dec 07 '16 at 23:50
  • A better experience would probably a more responsive app. Use the asynchronous process and let the user know the work is being done. On older devices, could take a longer time so I don't think you should rely on a synchronous process. – Swift Rabbit Sep 30 '20 at 13:20
6

For Swift 2, you can easily play the video with PHAsset using this method below,

Import File

import AVKit

From PHAsset

static func playVideo (view:UIViewController, asset:PHAsset) {

        guard (asset.mediaType == PHAssetMediaType.Video)

            else {
                print("Not a valid video media type")
                return
        }

        PHCachingImageManager().requestAVAssetForVideo(asset, options: nil, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) in

            let asset = asset as! AVURLAsset

            dispatch_async(dispatch_get_main_queue(), {

                let player = AVPlayer(URL: asset.URL)
                let playerViewController = AVPlayerViewController()
                playerViewController.player = player
                view.presentViewController(playerViewController, animated: true) {
                    playerViewController.player!.play()
                }
            })
        })
    }
Sazzad Hissain Khan
  • 29,428
  • 20
  • 134
  • 192
1

Import

import AVKit

Swift 5

let phAsset = info[UIImagePickerControllerPHAsset] as? PHAsset
    
PHCachingImageManager().requestAVAsset(forVideo: phAsset, options: nil) { (avAsset, _, _) in
    print(avAsset)
}
Yunus T.
  • 305
  • 4
  • 8
0

You can try this trick but it is handy when you have 3,4 or maybe 5 phassets that you want to convert to AVAsset :

    [[PHImageManager defaultManager] requestAVAssetForVideo:assetsArray[0] options:option resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
//do something with this asset
       [[PHImageManager defaultManager] requestAVAssetForVideo:assetsArray[1] options:option resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {
//so on...
       }
        }

So basically,you can call this method again when you have converted 1 phasset to AVAsset.I know this might not be an efficient code but it should not be forbidden for little purposes.

Reckoner
  • 889
  • 1
  • 10
  • 24
0

The following is a Swift 4 implementation that relies on a semaphore to make the request synchronously.

The code is commented to explain the various steps.

func requestAVAsset(asset: PHAsset) -> AVAsset? {
    // We only want videos here
    guard asset.mediaType == .video else { return nil }
    // Create your semaphore and allow only one thread to access it
    let semaphore = DispatchSemaphore.init(value: 1)
    let imageManager = PHImageManager()
    var avAsset: AVAsset?
    // Lock the thread with the wait() command
    semaphore.wait()
    // Now go fetch the AVAsset for the given PHAsset
    imageManager.requestAVAsset(forVideo: asset, options: nil) { (asset, _, _) in
        // Save your asset to the earlier place holder
        avAsset = asset
        // We're done, let the semaphore know it can unlock now
        semaphore.signal()
    }

    return avAsset
}
CodeBender
  • 30,010
  • 12
  • 103
  • 113
  • 1
    This use is not correct. avAsset return nil to always. Because requestAVAsset is a async func. You can use like that: https://stackoverflow.com/a/56670647/4644319 – Yunus T. Jun 19 '19 at 15:04