Overriding MPNowPlayingInfoCenter while using WKWe

2019-03-19 10:35发布

问题:

I am trying to create a multi-source music player with tracks coming from YouTube and Soundcloud, and I would like to override the content of MPNowPlayingInfoCenter to provide informations about the artists/releases instead of the name of the YouTube video.

Everything worked well when I used UIWebview, but for performance reasons, I had to switch to the new WKWebview and now the method I used before to set the nowPlayingInfos has no effect

Is there a way to disable the automatic mapping of the <audio> and <video> tags inside the HTML and/or to override the infos it provides with my infos?

Here's the code that I use which works on iOS 7 and worked on iOS 8 when I used UIWebview:

let newInfos = [
            MPMediaItemPropertyTitle: (currentPlaylist[currentPlaylistIndex] as! Track).trackName,
            MPMediaItemPropertyArtist: (currentPlaylist[currentPlaylistIndex] as! Track).trackArtist,
            MPMediaItemPropertyPlaybackDuration: NSNumber(integer: self.getDuration()),
            MPNowPlayingInfoPropertyElapsedPlaybackTime: NSNumber(integer: self.getCurrentTime()),
            MPNowPlayingInfoPropertyPlaybackRate: NSNumber(double: self.playing ? 1.0 : 0.0),
            MPMediaItemPropertyArtwork: MPMediaItemArtwork(image: image)
        ]

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = newInfos

I checked that none of the variables I use are nil, and I activated my AudioSession in the AppDelegate

var audioSession = AVAudioSession.sharedInstance()
var error : NSError?
audioSession.setCategory(AVAudioSessionCategoryPlayback, error: &error)
audioSession.setActive(true, error: &error)
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

Any ideas?

回答1:

Although it's impossible to override the property itself on iOS, it should be noted that changing the "title" property of the <video> element in JavaScript

I've achieved this behaviour with

var control = ...; //Create video DOM control
control.title = "Desired title";