本文介绍了Swift - 从iOS PHAsset播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想播放从iOS相册中收集的 PHAsset 中的视频。 PHAsset 视频 nsurl ()对我自己的应用程序有效,可以说 MyPlayer 一会儿/块。当我将图像/视频复制到 MyPlayers 自己的沙箱时, nsurl 始终有效。在我看来,

I want to play video from PHAsset, collected from iOS Photos. PHAsset video nsurl (https://stackoverflow.com/a/35099857/1084174) is valid to my own application lets say MyPlayer for few moments/blocks. When I am copying the image/video into MyPlayers own sandbox only then the nsurl becomes always valid. It seems to me,

如果是这种情况,所有其他玩家如何在飞行中播放长视频?如果有其他方式播放视频而不复制到应用程序沙盒,请告诉我。

If this is the case how do all other player play long videos on the fly? If there is any other way to play video without copying to apps sandbox, please let me know the way.

推荐答案

几天后实验最后我找到了解决方案,

After several days experiment finally I come upon a solution,

import AVKit



来自PHAsset



From 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



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

这篇关于Swift - 从iOS PHAsset播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 00:22