我使用自定义图像选择器来选择时间流逝,我想获取所选时间流逝的网址,以便我可以播放时间流逝。
- (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];
}
这就是我现在所拥有的,但我需要获得资产的网址。
我通常使用swift但这个自定义图像选择器是用objective-c编写的。所以我有点客观 - c菜鸟
更新的代码
- (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];
}
你应该使用PHImageManager
。
使用类的sharedInstance
并使用PHAsset
,options和完成块处理程序调用此方法:
- (PHImageRequestID)requestAVAssetForVideo:(PHAsset *)asset options:(PHVideoRequestOptions *)options resultHandler:(void (^)(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info))resultHandler
处理程序将为您提供AVAsset
,您应该使用AVPlayerItem
而不是URL。
例:
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
// Use the AVAsset avAsset
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
}];
注意它可能的异步会进一步干扰你的应用程序流。
顺便说一句,如果你想要实际的URL - 也就是说,UIImagePickerController在创建AVPlayerItem时传递给AVPlayerItem的那个,以及看起来像用于AVAssetReader / Writer的URL的那个 - 那么做这个:
[[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]);
}];
虽然phAsset是PHAsset对象,而avAsset是由PHImageManager生成的生成的AVAsset对象,但是从上面的代码输出到控制台将产生,例如:
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
对于Swift 3.0,您可以从PHAsset对象获取图像和视频的字符串URL
asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions(), completionHandler: { (contentEditingInput, dictInfo) in
if asset.mediaType == .image
{
if let strURL = contentEditingInput?.fullSizeImageURL?.absoluteString
{
print("IMAGE URL: ", strURL)
}
}
else
{
if let strURL = (contentEditingInput!.avAsset as? AVURLAsset)?.url.absoluteString
{
print("VIDEO URL: ", strURL)
}
}
})
在Swift 4.0和iOS 9.0+中正确回答@HirenPanchal
func getUrlFromPHAsset(asset: PHAsset, callBack: @escaping (_ url: URL?) -> Void)
{
asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions(), completionHandler: { (contentEditingInput, dictInfo) in
if let strURL = (contentEditingInput!.audiovisualAsset as? AVURLAsset)?.url.absoluteString
{
PRINT("VIDEO URL: \(strURL)")
callBack(URL.init(string: strURL))
}
})
}
在PHAsset
的视频网址中,我曾经在Swift 2上准备了一个实用功能(播放来自PHAsset
的视频)。分享这个答案,可能对某人有帮助。
static func playVideo (view:UIViewController, asset:PHAsset)
请检查这个Answer
let imagePicker = QBImagePickerController()
imagePicker.delegate = self
imagePicker.numberOfColumnsInPortrait = 2
imagePicker.maximumNumberOfSelection = 2
imagePicker.allowsMultipleSelection = true
imagePicker.mediaType = .video
self.present(imagePicker, animated: true, completion: nil)
func qb_imagePickerController(_ imagePickerController:
QBImagePickerController!, didFinishPickingAssets assets: [Any]!) {
for asset in assets
{
PHCachingImageManager().requestAVAsset(forVideo: asset as! PHAsset,
options: nil, resultHandler: {(asset, audioMix, info) -> Void in
if asset != nil {
let avasset = asset as! AVURLAsset
let urlVideo = avasset.url
yourarray.add(urlVideo as URL)
}
})
}
imagePickerController.dismiss(animated: true) {
//You can access video urls here
print("Total Videos:\(yourarray.count)")
}
}