Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 避免AVPlayer和UIWebView同时播放音乐_Ios_Avfoundation_Avplayer - Fatal编程技术网

Ios 避免AVPlayer和UIWebView同时播放音乐

Ios 避免AVPlayer和UIWebView同时播放音乐,ios,avfoundation,avplayer,Ios,Avfoundation,Avplayer,谢谢你注意到这个问题。我正在通过AVPlayer在我的应用程序中使用一个单件播放音乐,如下所示: AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayback error:nil]; [session setActive:YES error:nil]; AVURLAsset *musicAsset = [[AVURLAsset a

谢谢你注意到这个问题。我正在通过
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
?谢谢

您是否尝试过侦听AVPlayerItemCurrentNotification?当您的网页启动媒体文件(视频或音频)时,该文件将被触发。您可以按如下方式拦截它:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(avPlayerItemBecameCurrent:)
                                             name:@"AVPlayerItemBecameCurrentNotification"
                                           object:nil];
现在(我意识到我在这里教你吃蛋-我道歉。这只是为了完整性)你需要AVPlayerTime:

-(void)avPlayerItemBecameCurrent:(NSNotification*)notification
{
    // Your code goes here to stop your native AVPlayer from playing.
}
警告-我还没有测试过这个,但它应该可以工作


(写完这篇文章后,再翻了一番,我发现了这篇文章)

嗨,杭,你有什么解决办法吗?