What to do for Video PHAssets

2.4k views Asked by At

I see that for PHContentEditingInput (https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHContentEditingInput_Class/index.html#//apple_ref/occ/instp/PHContentEditingInput/avAsset), the Video Asset stuff has been deprecated.

If I need to process videos, what should I be using instead?

(In particular, NSURL from PHAsset, they used that deprecated function here. Is there something else I could use that's not deprecated but can attain the NSURL?)

EDIT: So... after I read up a bit on AVFoundation framework... How exactly do we convert our PHAsset into an AVURLAsset without using the deprecated method?

4

There are 4 answers

0
rickster On

The documentation hasn't been updated for iOS 9 (yet, I'd assume). But if you look in the PHContentEditingInput.h header (or generated Swift interface) in Xcode, though, you'll see that it's just a renaming — avAsset is deprecated in favor of audiovisualAsset. It still returns an AVAsset object, which you can then process using AVFoundation.

4
matt On

If I need to process videos, what should I be using instead

It depends what you mean by "process", I suppose, but in general you should be using AVFoundation.

0
James Bush On

What kind of URL are you looking for from the AVURLAsset object? There are two variants, and the distinction is always relevant (take the URL generated by UIImagePickerController versus the one used by AVAssetReader/Writer, for example). Here they are:

[[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
    NSURL *url = (NSURL *)[[(AVURLAsset *)avAsset URL] fileReferenceURL];
    NSLog(@"url = %@", [url absoluteString]);
    NSLog(@"url = %@", [url relativePath]);
 }];

Whereas phAsset is the PHAsset object, and avAsset is the resulting AVAsset object generated by PHImageManager, the output to the console from the above code will produce, for example:

2016-04-16 01:15:40.155 ChromaEpsilon[3423:933358] url = file:///.file/id=16777218.8262005
2016-04-16 01:15:40.155 ChromaEpsilon[3423:933358] url = /private/var/mobile/Media/DCIM/108APPLE/IMG_8421.MOV

There's more than just these two, I believe, but start here.

0
Sazzad Hissain Khan On

For Swift 2.0

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   // you are looking for

            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()
                }
            })
        })
    }