【问题标题】:Avoid AVPlayer and UIWebView play music in the same time避免 AVPlayer 和 UIWebView 同时播放音乐
【发布时间】:2014-01-21 08:34:33
【问题描述】:

感谢您注意到这个问题。我正在使用单例通过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),所以用户也可以访问网页,并在那里播放音乐。 UIWebViewAVPlayer的音乐可以同时播放,似乎很奇怪。有什么方法可以检测UIWebView 是否会播放音乐,所以我可以停止原生AVPlayer?谢谢!

【问题讨论】:

  • 嗨,Hang,你有解决方案吗?

标签: ios avfoundation avplayer


【解决方案1】:

您是否尝试过监听 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.
}

警告 - 我没有对此进行测试,但它应该可以工作。

(写了这个,又挖了一点,我找到了这个Detecting and intercepting video playback in UIWebView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2013-10-19
    • 2017-05-13
    • 1970-01-01
    相关资源
    最近更新 更多