I am using a custom image picker to pick a time lapse and I want to get the url of the picked time lapse so I can then play the time lapse.
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
for (PHAsset *asset in assets) {
NSURL *url = [NSURL URLWithString: assetURLgoesHere];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[videoPlayer play];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
This is what I have at the moment but I need to get the url of the asset.
I usually use swift but this custom image picker is written in objective-c. so I am a bit of a objective-c noob
Updated code
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
for (PHAsset *asset in assets) {
- (PHImageRequestID)requestAVAssetForVideo:(PHAsset *)asset options:(PHVideoRequestOptions *)options resultHandler:(void (^)(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info))resultHandler
NSURL *url = [NSURL URLWithString: asset];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[videoPlayer play];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
By the way, if you want the actual URL--that is to say, the same one that UIImagePickerController passes to an AVPlayerItem when it is created, and the same one that looks like the URL one would use with AVAssetReader/Writer--then do this:
[[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