6

我正在使用自定义图像选择器来选择时间流逝,我想获取所选择的时间流逝的 url,这样我就可以播放时间流逝了。

- (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];
}

这是我目前拥有的,但我需要获取资产的 url。

我通常使用 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];
}
4

6 回答 6

9

顺便说一句,如果您想要实际的 URL——也就是说,与 UIImagePickerController 在创建 AVPlayerItem 时传递给它的 URL 相同,并且看起来像与 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
于 2016-04-16T08:19:55.883 回答
7

你应该使用PHImageManager.

使用该类并通过您的、 选项和完成块处理程序sharedInstance调用此方法:PHAsset

- (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];
}];

当心它可能会进一步干扰您的应用程序流程的异步。

于 2015-11-22T18:30:18.393 回答
5

对于 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)
      }
    }
  })
于 2016-12-16T06:32:43.427 回答
4

Swift 4.0(也适用于 Swift 5.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))
        }
    })
}
于 2018-08-30T06:49:28.570 回答
1

从 QBImagePickerController 中的 didFinishPickingAssets 从 Swift 4.2 中的 PHAsset 获取视频 URL

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)")
    }
    
 }
于 2019-04-24T13:15:41.017 回答
0

在谈到来自 的视频 url 时PHAsset,我曾经在Swift 2上准备了一个实用功能(播放来自 的视频PHAsset)。在这个答案中分享,可能会对某人有所帮助。

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

请检查这个答案

于 2016-09-20T21:47:19.613 回答