i have 3 urls , have check if these urls have video link play or not. have esc link if not contain video in url , play video next url. following code track playing state.
let playerav = avplayerviewcontroller() var player = avplayer() playerav.player = player playerav.view.frame = cgrectmake(0, 0, self.videoview.frame.width, self.videoview.frame.height) self.addchildviewcontroller(playerav) self.videoview.addsubview(playerav.view) playerav.didmovetoparentviewcontroller(self) playerav.player?.play() addobserverofmovieplayer() nsnotificationcenter.defaultcenter().addobserver(self, selector: #selector(channeldetailviewcontroller.notificationobserver(_:)), name:avplayeritemdidplaytoendtimenotification , object: player.currentitem) _ = uidevice.begingeneratingdeviceorientationnotifications nsnotificationcenter.defaultcenter().addobserver(self, selector: #selector(channeldetailviewcontroller.deviceorientationdidchange(_:)) , name: uideviceorientationdidchangenotification, object: nil) playerav.player!.addobserver(self, forkeypath: "rate", options: nskeyvalueobservingoptions.new, context: nil) playerav.player!.currentitem!.addobserver(self, forkeypath: "playbackbufferempty", options: nskeyvalueobservingoptions.new, context: nil) playerav.player!.currentitem!.addobserver(self, forkeypath: "playbacklikelytokeepup", options: nskeyvalueobservingoptions.new, context: nil) override func observevalueforkeypath(keypath: string?, ofobject object: anyobject?, change: [string : anyobject]?, context: unsafemutablepointer<void>) { if keypath == "rate" { if let rate = change?[nskeyvaluechangenewkey] as? float { if playerav.player!.currentitem!.status == avplayeritemstatus.readytoplay{ if rate != 0 && playerav.player!.error == nil { print("normal playback") timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: #selector(channeldetailviewcontroller.somaction), userinfo: nil, repeats: true) } else{ timer?.invalidate() print("movie player stopped") } }else if playerav.player!.currentitem?.status == avplayeritemstatus.unknown{ timer?.invalidate() print("not ready play") } } } if keypath == "playbackbufferempty"{ if playerav.player?.currentitem?.playbackbufferempty == true{ timer?.invalidate() print("movie player stopped due no buffer") } } if keypath == "playbacklikelytokeepup" { if playerav.player?.currentitem?.playbacklikelytokeepup == true{ print("movie player playing after enough data in buffer") playerav.player?.play() } } }
just check length of video if it's invalid length return 0 code :
let assets = avasset(url: nsurl(fileurlwithpath: filepath)) let length=float(assets.duration.value)/float(assets.duration.timescale) if length != 0.0 { // play }
if have still problem can ask me anytime.
Comments
Post a Comment