大佬教程收集整理的这篇文章主要介绍了ios – 如何关闭以前的AVPlayer和AVPlayerItem,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
//set variables for video play var playerItem:AVPlayerItem? var player:AVPlayer? //variables that contain video file path,name and extension var videoPath = NSBundle.mainBundle().resourcePath! var videoName = "blue" let videoExtension = ".mp4" //DISPLAY VIDEO func showVideo(){ //Assign url path let url = NSURL(fileURLWithPath: videoPath+"/Base.lproj/"+videoName+videoExtension) playerItem = AVPlayerItem(URL: url) player=AVPlayer(playerItem: playerItem!) let playerLayer=AVPlayerLayer(player: player!) //setplayser LOCATIOn in uiview and show video playerLayer.frame=CGRectMake(700,5,350,350) self.view.layer.addSublayer(playerLayer) player!.play() // Add notification to kNow when the video ends,then replay it again. THIS IS A CONTinUAL LOOP NsnotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification,object: player!.currentItem,queue: nil) { notification in let t1 = CMTimeMake(5,100); self.player!.seektotime(t1) self.player!.play() } }
`
`
//set variables for video play var playerItem:AVPlayerItem? var player:AVPlayer? var playerLayer = AVPlayerLayer() //NEW playerLayer var LOCATIOn //variables that contain video file path,name and extension var videoPath = NSBundle.mainBundle().resourcePath! var videoName = "blue" let videoExtension = ".mp4" var createLayerSwitch = true /*NEW switch to say whether on not to create the layer when referenced by the closePlayer and showVideo functions*/ //DISPLAY VIDEO func showVideo(){ //Assign url path let url = NSURL(fileURLWithPath: videoPath+"/Base.lproj/"+videoName+videoExtension) playerItem = AVPlayerItem(URL: url) player=AVPlayer(playerItem: playerItem!) playerLayer=AVPlayerLayer(player: player!) //NEW: remove 'let' from playeLayer here. //setplayser LOCATIOn in uiview and show video playerLayer.frame=CGRectMake(700,350) self.view.layer.addSublayer(playerLayer) player!.play() createLayerSwitch = false //NEW switch to tell if a layer is already created or not. I set the switch to false so that when the next tapped item/button references the closePlayer() function,the condition is triggered to close the player and the layer // Add notification to kNow when the video ends,then replay it again without a pause between replays. THIS IS A CONTinUAL LOOP NsnotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification,queue: nil) { notification in let t1 = CMTimeMake(5,100); self.player!.seektotime(t1) self.player!.play() } } //NEW function to kill the current player and layer before playing the next video func closePlayer(){ if (createLayerSwitch == falsE) { player!.pause() player = nil playerLayer.removefromsuperlayer() } }
`
以上是大佬教程为你收集整理的ios – 如何关闭以前的AVPlayer和AVPlayerItem全部内容,希望文章能够帮你解决ios – 如何关闭以前的AVPlayer和AVPlayerItem所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。