Overriding MPNowPlayingInfoCenter while using WKWebView

扶醉桌前 提交于 2019-12-20 20:26:46

问题


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";


来源:https://stackoverflow.com/questions/30029338/overriding-mpnowplayinginfocenter-while-using-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!