Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding MPNowPlayingInfoCenter while using WKWebView

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?

like image 251
Marc Peysale Avatar asked May 04 '15 11:05

Marc Peysale


1 Answers

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";
like image 120
Mislav Javor Avatar answered Oct 05 '22 04:10

Mislav Javor