我有一個AVPlayer,我加載到一個新的視圖,每當一個鏈接被點擊。無法刪除或停止AVPlayer
-(void)createAndConfigurePlayerWithURL:(NSURL *)movieURL sourceType:(MPMovieSourceType)sourceType {
self.playerItem = [AVPlayerItem playerItemWithURL:movieURL];
customControlOverlay = [[AFDetailViewController alloc] initWithNibName:@"AFMovieScrubControl" bundle:nil];
backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[customControlOverlay.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:customControlOverlay.view];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:[AVPlayer playerWithPlayerItem:playerItem]];
[playerLayer.player play];
playerLayer.frame = customControlOverlay.view.frame;
[customControlOverlay.view.layer addSublayer:playerLayer];
}
上面的代碼將AVPlayer添加到我的應用程序,並正常工作。我在我的customControlOverlay筆尖中有一個切換,它應該刪除視圖並停止播放AVplayer。
-(IBAction)toggleQuality:(id)sender {
if (qualityToggle.selectedSegmentIndex == 0) {
NSLog(@"HD");
[playerLayer.player pause];
[self.view removeFromSuperview];
} else if (qualityToggle.selectedSegmentIndex == 1) {
NSLog(@"SD");
}
}
該視圖被正確刪除,但播放器仍在後臺播放。經過測試後,玩家不會響應toggleQuality方法中的任何代碼,但我在那裏獲得字符串,因爲支票正在記錄。
有關我在做什麼錯的任何想法?
使用playerLayer.player = nil;在從超級視圖中移除之前 –
即使我這樣做,聲音仍在播放。 – AFraser
如果你以其他順序做(如果你刪除然後設置爲零)會怎麼樣? – Squatch