[英]Avoid AVPlayer and UIWebView play music in the same time
感謝您注意到這個問題。 我正在使用單身人士通過AVPlayer
在我的應用中播放音樂,就像這樣:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
AVURLAsset *musicAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:musicAsset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:_playerItem];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[player play];
它工作正常。 但我的應用程序有一些音樂頁面(HTML5),因此用戶也可以訪問網頁,並在那里播放音樂。 來自UIWebView
和AVPlayer
的音樂可以在同一時間播放,似乎很奇怪。 有沒有辦法檢測UIWebView
是否會播放音樂,所以我可以停止原生AVPlayer
? 謝謝!
您是否嘗試過收聽AVPlayerItemBecameCurrentNotification? 當您的網頁啟動媒體文件(視頻或音頻)時,將觸發此操作。 您可以按如下方式攔截它:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(avPlayerItemBecameCurrent:)
name:@"AVPlayerItemBecameCurrentNotification"
object:nil];
現在(我意識到我教你在這里吮雞蛋 - 道歉。這只是為了完整)你需要avPlayerItemBecameCurrent:
-(void)avPlayerItemBecameCurrent:(NSNotification*)notification
{
// Your code goes here to stop your native AVPlayer from playing.
}
警告 - 我沒有測試過這個,但它應該有效。
(寫完這篇文章后,經過深入挖掘后,我在UIWebView中發現了這個檢測和攔截視頻播放 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.