gpt4 book ai didi

ios - 如何在 iOS 中播放视频 PHAsset?

转载 作者:IT王子 更新时间:2023-10-29 05:21:06 26 4
gpt4 key购买 nike

我想播放来自 PHAsset 的视频,从 iOS 照片中收集。 PHAsset 视频 nsurl ( https://stackoverflow.com/a/35099857/1084174 ) 对我自己的应用程序有效让我们说 MyPlayer 片刻/ block 。当我将图像/视频复制到 MyPlayers 自己的沙箱时,只有 nsurl 才会始终有效。在我看来,

I need to copy each video from temporary PHAsset nsurl to MyPlayers sandbox (appgroup/documents) and only then I can play the video with sandbox relative nsurl.

如果是这种情况,所有其他播放器如何即时播放长视频?如果有任何其他方式可以在不复制到应用程序沙箱的情况下播放视频,请告诉我方式。

最佳答案

经过几天的实验终于找到了解决方案,

导入文件

import AVKit

来自 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

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

来自 AppLocalUrl

static func playVideo (view:UIViewController, appLocalUrl:NSURL) {

dispatch_async(dispatch_get_main_queue(), {

let player = AVPlayer(URL: appLocalUrl)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
view.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
})
}

关于ios - 如何在 iOS 中播放视频 PHAsset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39349949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com