如何从 WKWebView 获取当前音频 URL 在我的应用程序中播放?

How can I get current audio URL playing inside my app from WKWebView?

我有一个 WKWebView,我正在使用

检查音频是否正在播放
[[AVAudioSession sharedInstance] isOtherAudioPlaying] 

我试图通过以下方式获取有关它的一些信息:

  [MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo]

但它是零。

然后我尝试了:

 [[MPMusicPlayerController systemMusicPlayer] nowPlayingItem]

也是零。

如何获取 URL 当前正在播放的音频?

因为您已经可以检测到何时播放音频,所以我认为适合您的解决方案是从 WKNavigationDelegate[=16= 中实施 decidePolicyForNavigationAction 方法].

您可以组合此委托方法提供的信息,只要单击与您已经完成的音频检测关联的 link,就会调用该方法。

您需要订阅通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemBecameCurrent:)
                                             name:@"AVPlayerItemBecameCurrentNotification"
                                           object:nil];

然后在方法里面做这个playerItemBecameCurrent

-(void)playerItemBecameCurrent:(NSNotification*)notification {
AVPlayerItem *playerItem = [notification object];
if(playerItem == nil) return;
// Break down the AVPlayerItem to get to the path
AVURLAsset *asset = (AVURLAsset*)[playerItem asset];
NSURL *url = [asset URL];
NSString *path = [url absoluteString];}