在iOS上实时视频清理

时间:2012-09-29 19:03:02

标签: iphone objective-c ios ipad video

Media Foundation中的标准电影播放器​​似乎不支持非常流畅的擦洗。我想这样做,以便当您将手指滑过条形图时,显示的视频会不断刷新,而不是像默认情况下每隔几秒跳一次。起初我以为默认控件可能已经内置了某种延迟,但使用我自己的滑块来设置播放时间并没有帮助。有什么想法吗?

编辑:我还在试验,看起来可能的磨砂起点之间有一个设定的间隔。也许有办法减少间隔。

1 个答案:

答案 0 :(得分:2)

这对我有用。我在幻灯片更改时从视频资产中抓取一个帧。见下面的代码。它是从我当前的项目中删除的,可能包含som错误但你会得到这个想法。它在Swift 1中虽然希望没问题。

var urlVideo :NSURL = NSURL()
var vidLength :CMTime = CMTime()
var durInSeconds: Float = 0.00
var timescale:CMTimeScale = CMTimeScale()
var videoInfo:NSDictionary = NSDictionary()
var chosePosSlider:UISlider = UISlider()

@IBOutlet weak var imgCont: UIImageView!

func generateThumnail(url : NSURL, fromTime:Float64) -> UIImage {

    var asset :AVAsset = AVAsset.assetWithURL(url) as! AVAsset
    var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)

    assetImgGenerate.appliesPreferredTrackTransform = true
    assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
    assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;

    var error       : NSError? = nil
    var time        : CMTime = CMTimeMakeWithSeconds(fromTime, timescale)
    var img         : CGImageRef = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil, error: &error)
    var frameImg    : UIImage = UIImage(CGImage: img)!

    return frameImg

}

func sliderValueDidChange(sender:UISlider!) {

    var newTime:Float = durInSeconds * sender.value
    setFrame(newTime)

}

func setFrame(pos:Float) {
    imgCont.image = generateThumnail(urlVideo, fromTime: Float64(pos))
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    self.dismissViewControllerAnimated(true, completion: nil)

    videoInfo = info as NSDictionary
    urlVideo = videoInfo.objectForKey(UIImagePickerControllerMediaURL) as! NSURL

    var asset :AVAsset = AVAsset.assetWithURL(urlVideo) as! AVAsset

    vidLength = asset.duration
    durInSeconds = Float(CMTimeGetSeconds(vidLength))
    timescale = vidLength.timescale

    chosePosSlider = UISlider(frame:CGRectMake(20, 450, 340, 20))
    chosePosSlider.minimumValue = 0
    chosePosSlider.maximumValue = 1
    chosePosSlider.continuous = true
    chosePosSlider.tintColor = UIColor.greenColor()
    chosePosSlider.value = 0
    chosePosSlider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
    self.view.addSubview(chosePosSlider)


    setFrame(0.0)

}