2016-02-01 53 views
0

我有表單視圖,其中單元格從Parse加載視頻對象。 加載視頻時,playButton會出現在視頻的靜態圖像表示上。播放按鈕是故事板UIButton的出口。當playButton被推動時暫停AVPlayer的其他實例

在我FeedCell.m:

- (IBAction)playButtonTapped:(id)sender { 
[self.player play]; 

if (self.player.rate != 0 && (self.player.error == nil)) { 
    // player is playing 
    self.playButton.hidden = YES; 
} 

self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector: @selector (playerItemDidReachEnd:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
              object:[self.player currentItem]]; 
} 

- (void)playerItemDidReachEnd:(NSNotification *)notification { 
self.playButton.hidden = NO; 
[self.playerItem seekToTime:kCMTimeZero]; 
[self.player pause]; 
} 

我想添加到這個按鈕的功能,這樣當爲playButton被竊聽的AVPlayerItem將發揮和AVPlayers的其他所有實例都將被暫停。

我可以通過設置另一個NSNotification來做到這一點嗎?

回答

0

NSNotification不是答案,因爲它向所有添加爲觀察者的接收者發送廣播消息,在這種情況下,每個單元包含AVPlayers。

但存在一個問題:只要向下滾動UITableView,一些UITableViewCell就會脫離屏幕,即使它們被添加爲觀察者也不會收到任何NSNotification。

所以你可以添加所有的AVPlayers到一個NSMutableArray,當你想停止所有的視頻,你可以循環陣列,並手動調用停止方法。 然後從陣列中刪除它們。 請記住,將AVPlayers添加到數組會導致它們的引用計數永遠不會達到0,因此它們在數組處於活動狀態時不會被釋放。 因此,把NSMutableArray放在包含UITableView的同一個控制器上,所以當它被釋放時,該數組也被釋放,然後所有的AVPlayers被解除分配

+0

有沒有一種方法可以影響已經滾動出視野的單元? –

+0

您可以改爲使用'AVQueuePlayer'。這會讓你預先加載資源,然後隨時按下playButton,它會停止正在播放的資產並轉到新的資產 – MikeG

相關問題