1

I have a WKWebView and I am checking if audio is playing using

[[AVAudioSession sharedInstance] isOtherAudioPlaying] 

I tried to get some information about it using :

  [MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo]

but it was nil.

Then I tried:

 [[MPMusicPlayerController systemMusicPlayer] nowPlayingItem]

and it was also nil.

How can I get URL of current playing audio?

AtWork
  • 1,283
  • 1
  • 14
  • 34

2 Answers2

1

You need to subscribe on notification :

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

And then do this inside method 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];}
0

As you can already detect when the audio is being played I think that the solution for you would be implementing decidePolicyForNavigationAction method from the WKNavigationDelegate.

You can combine the information provided by this delegate method that will be called whenever a link is clicked associated with the audio detection the you have already done.

kallahir
  • 21
  • 4
  • No, this is not working for audio-buttons.When you klick them this method is not calling. –  Feb 25 '17 at 13:33
  • Please share a little bit more information of exactly what you're trying to do, because I think that I misunderstood what you're trying to do. – kallahir Feb 26 '17 at 23:35
  • Well.I am trying to make kind of file-downloader using built-in WKWebView.User clicks on a button to play a song or a video and I show him alert asking if he wants to download this audio or video.I gave more information in this question http://stackoverflow.com/questions/42459538/method-decidepolicyfornavigationaction-is-not-called-some-times and i gave a link for the app that does this functionality –  Feb 26 '17 at 23:40
  • Sorry I wouldn't know how to help you in the cenario described. – kallahir Mar 01 '17 at 15:20
  • If you could please share it here, it would be awesome. – kallahir Mar 01 '17 at 15:44
  • 1
    I will in a few hours –  Mar 01 '17 at 15:45