我被困住的地方是,无论何时显示下一个视频,之前的视频都会保持打开状态.此外,播放了16个视频后,播放器完全消失在我的iPad上.我已尝试过在本网站和其他网站上提出的许多建议,但要么swift发现它们有问题,要么就是不行.
所以问:在我的代码中,我怎么告诉它“嘿,下一个视频已经开始播放,删除之前的视频和它的图层并释放内存,这样我就可以播放所需数量的视频”?
//set variables for vIDeo playvar playerItem:AVPlayerItem?var player:AVPlayer?//variables that contain vIDeo file path,name and extensionvar vIDeoPath = NSBundle.mainBundle().resourcePath!var vIDeoname = "blue"let vIDeoExtension = ".mp4"//disPLAY VIDEOfunc 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() }}
`
解决方法 我改编了@Anupam Mishra的Swift代码建议.它最初没有工作,但最终我认为我必须将playerLayer带到该功能之外并在我将玩家设置为nil之后关闭playerLayer.也.而不是使用毫无疑问仍然有用的’if(player!.rate> 0 …)’,我创建了一个变量开关来指示何时说“杀死玩家和图层”,如下所示.它可能不漂亮,但它的工作原理!以下是像我这样的绝对新手 – 我从这次经历中学到了什么:在我看来,ios设备只允许将16层添加到vIEwController(或superLayer).因此,在使用其播放器打开下一个图层之前,需要删除每个图层,除非您确实希望一次运行16个图层.这个代码实际上对你来说是什么:这个代码在现有的vIEwController上创建一个可调整大小的层,并在无限循环中播放你的包中的视频.当即将调用下一个视频时,将完全删除当前视频和图层,释放内存,然后播放带有新视频的新图层.视频图层大小和位置可使用playerLayer.frame = CGRectMake(左,顶部,宽度,高度)参数完全自定义.如何完成所有工作:假设您已将视频添加到捆绑包中,请为按钮点击创建另一个功能.在该函数中,首先调用’stopPlayer()’函数,将’vIDeoname’变量更改为您想要的新视频名称,然后调用’showVIDeo()’函数. (如果您需要更改视频扩展名,请将’vIDeoExtension’从let更改为var.`
//set variables for vIDeo playvar playerItem:AVPlayerItem?var player:AVPlayer?var playerLayer = AVPlayerLayer() //NEW playerLayer var location//variables that contain vIDeo file path,name and extensionvar 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 VIDEOfunc 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 vIDeofunc closePlayer(){ if (createLayerSwitch == false) { player!.pause() player = nil playerLayer.removefromsuperlayer() }}
`
总结以上是内存溢出为你收集整理的ios – 如何关闭以前的AVPlayer和AVPlayerItem全部内容,希望文章能够帮你解决ios – 如何关闭以前的AVPlayer和AVPlayerItem所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)