我正在使用自定义图像选择器来选择时间流逝,我想获取所选时间流逝的网址,以便我可以播放时间流逝。
- (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];
}
答案 0 :(得分:8)
顺便说一句,如果你想要实际的URL - 也就是说,UIImagePickerController在创建时传递给AVPlayerItem的那个URL,以及看起来像URL的那个URL将与AVAssetReader / Writer一起使用 - 然后这样做:
[[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
答案 1 :(得分:6)
您应该使用PHImageManager
。
使用班级sharedInstance
并使用PHAsset
,选项和完成块处理程序调用此方法:
- (PHImageRequestID)requestAVAssetForVideo:(PHAsset *)asset options:(PHVideoRequestOptions *)options resultHandler:(void (^)(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info))resultHandler
处理程序会为您AVAsset
提供AVPlayerItem
,而不是网址。
示例:
[[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];
}];
请注意它可能存在异步,这会进一步干扰您的应用程序流程。
答案 2 :(得分:4)
对于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)
}
}
})
答案 3 :(得分:0)
在PHAsset
的视频网址中,我曾经在 Swift 2 (播放来自PHAsset
的视频)上准备了一个效用函数。分享这个答案,可能对某人有帮助。
static func playVideo (view:UIViewController, asset:PHAsset)
请检查此Answer
答案 4 :(得分:0)
在Swift 4.0和iOS 9.0+中@HirenPanchal的正确答案
startPayment().then()
startPayment(): Promise {
//...
return payNow.then(() => {
return createOrder();
});
}
createOrder(): Promise {
// ...
return itemRef.push...
}
答案 5 :(得分:0)
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)")
}
}