2016-07-25 47 views
1

我試圖在AVPlayer開始播放音樂時停止activityIndi​​cator,並且在AVPlayer再次啓動時(加載,緩衝)啓動activityIndi​​cator。這一點起作用,問題在於播放音樂之前,AVPlayer在幾秒鐘之前停止activityIndi​​cator(5,6,7)。當它再次(加載,緩衝)時,它不會再次啓動activityIndi​​cator。任何人都知道我的錯誤在哪裏,或者我需要修復它。由於AVPlayer在播放前停止UIActivityIndi​​cator

var activityView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge) 

var selectIndex:Int = -1 

var check = true 
var url : String! 
var playerItem:AVPlayerItem? 
var player:AVPlayer? 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RadioCollectionViewCell 
    cell.backgroundColor = UIColor.yellowColor() 

    let object = objects[indexPath.row] 
    cell.img.image = UIImage(named: object["image"]!) 
    cell.btnPlay.addTarget(self, action: Selector("audioControlButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside) 
    cell.btnPlay.tag = indexPath.row+1 


    return cell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){ 
    print("You selected cell #\(indexPath.item)!") 
} 

func audioControlButtonAction(sender: UIButton){ 

    if check == false { 
     deallocObservers(player!) 
    } 



    var btn:NSInteger 
    btn = sender.tag as NSInteger 

    let object = objects[btn-1] 
    let nurl = NSURL(string: "\(object["url"]!)")! 
    playerItem = AVPlayerItem(URL: nurl) 
    player=AVPlayer(playerItem: playerItem!) 

    print(selectIndex) 
    if selectIndex != -1 && selectIndex != sender.tag 
    { 
     let bt:UIButton = self.view.viewWithTag(selectIndex) as! UIButton 

     if bt.selected == true 
     { 
      bt.selected = false 
     } 
    } 

    if sender.selected == false{ 
     player!.addObserver(self, forKeyPath: "status", options:NSKeyValueObservingOptions(), context: nil) 
     player!.addObserver(self, forKeyPath: "playbackBufferEmpty", options:NSKeyValueObservingOptions(), context: nil) 
     player!.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options:NSKeyValueObservingOptions(), context: nil) 
     player!.addObserver(self, forKeyPath: "playbackBufferFull", options:NSKeyValueObservingOptions(), context: nil) 
     player!.addObserver(self, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions(), context: nil) 
     player!.play() 
     sender.selected = true 
     check = false 
     selectIndex = sender.tag 
     activityView.startAnimating() 


    } 
    else{ 
     activityView.stopAnimating() 
     check = true 
     player?.pause() 
     sender.selected = false 
     selectIndex = -1 
    } 

    print(selectIndex) 

} 

func deallocObservers(player: AVPlayer) { 

    player.removeObserver(self, forKeyPath: "status") 
    player.removeObserver(self, forKeyPath: "playbackBufferEmpty") 
    player.removeObserver(self, forKeyPath: "playbackLikelyToKeepUp") 
    player.removeObserver(self, forKeyPath: "loadedTimeRanges") 
    player.removeObserver(self, forKeyPath: "playbackBufferFull") 

} 


override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>){ 

    if object?.isEqual(player) == true && keyPath == "status" { 

     print(player?.status) 

     switch player!.status { 

      case AVPlayerStatus.Failed: 
       print("Player item status failed") 
       player?.play() 
      break 

      case AVPlayerStatus.ReadyToPlay: 
       print("Player item status is ready to play") 
       activityView.stopAnimating() 
      break 

      case AVPlayerStatus.Unknown: 
       print("player item status is unknown") 
      break 
     } 

     switch keyPath! { 

      case "playbackBufferFull": 
       activityView.stopAnimating() 
       print("playbackBufferFull") 
      break 

      case "playbackLikelyToKeepUp": 
       activityView.stopAnimating() 
       print("playbackLikelyToKeepUp") 
      break 

      case "playbackBufferEmpty": 
       activityView.startAnimating() 
       print("playbackBufferEmpty") 
      break 

      case "loadedTimeRanges": 
       print("loadedTimeRanges") 

      default: 
       print("Error") 
      break 
     } 
    } 
} 

}

輸出

AVPlayer開始後(5,6,7秒)玩這一行Player item status is ready to play

-1 
1 
Optional(__C.AVPlayerStatus) 
Player item status is ready to play 
Error 
1 
-1 
-1 
1 
Optional(__C.AVPlayerStatus) 
Player item status is ready to play 
Error 

enter image description here

回答

0

那麼在你的代碼中,無論何時按下一個單元格,你都有activityView.stopAnimating()。所以無論如何,要麼動畫將開始或停止點擊,這取決於if sender.selected == false

所以我認爲如果你刪除activityView.stopAnimating()那麼它不會停止這麼早的動畫。閱讀你的問題和代碼是很難的,因爲我不確定輸出是從哪裏來的。

+0

第一次當我按播放按鈕只是該代碼執行'如果sender.selected == false {............}''audioControlButtonAction'按鈕? – ZAFAR007

0

關於第二個問題,即「並且還沒有再次啓動activityIndi​​cator當它再次(加載,緩衝)」

我覺得應該是一個解決辦法可能是下列之一: -

一)

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){ 
    print("You selected cell #\(indexPath.item)!") 
//Start activityView Animation when user clicks on any item which won't be buffered by default 
activityView.startAnimating() 
} 

b)

var selectIndex:Int = -1 { 
didSet { 
     //Start activityView Animation when user clicks on any item which won't be buffered by default 
     activityView.startAnimating() 
     } 
} 
+0

不,我的意思是第一次顯示activityIndi​​cator也會在播放音樂時停止activityIndi​​cator,但當下一次需要緩存或再次加載時,由於互聯網連接速度緩慢或由於其他原因而無法啓動「startAnimating」。 – ZAFAR007

+0

我認爲問題出現在'observeValueForKeyPath'中,因爲它總是進入'AVPlayerStatus.ReadyToPlay'或'default:'? – ZAFAR007

+0

也許我不太確定,添加斷點並查看執行情況如何確認問題是否出現在observerValueForKeyPath或其他位置。另外,如果由於連接速度較慢,還包括可用性檢查連接可用性 – MShah