Skip to content

Instantly share code, notes, and snippets.

@Hamuko
Created April 16, 2021 22:20
Show Gist options
  • Save Hamuko/2a455a0a384ffa8dc1946aff5bf1acec to your computer and use it in GitHub Desktop.
Save Hamuko/2a455a0a384ffa8dc1946aff5bf1acec to your computer and use it in GitHub Desktop.
import Cocoa
import MediaPlayer
let bundle = CFBundleCreate(kCFAllocatorDefault, NSURL(fileURLWithPath: "/System/Library/PrivateFrameworks/MediaRemote.framework"))
let MRMediaRemoteRegisterForNowPlayingNotificationsPointer = CFBundleGetFunctionPointerForName(
bundle, "MRMediaRemoteRegisterForNowPlayingNotifications" as CFString
)
typealias MRMediaRemoteRegisterForNowPlayingNotificationsFunction = @convention(c) (DispatchQueue) -> Void
let MRMediaRemoteRegisterForNowPlayingNotifications = unsafeBitCast(MRMediaRemoteRegisterForNowPlayingNotificationsPointer, to: MRMediaRemoteRegisterForNowPlayingNotificationsFunction.self)
let MRMediaRemoteGetNowPlayingInfoPointer = CFBundleGetFunctionPointerForName(
bundle, "MRMediaRemoteGetNowPlayingInfo" as CFString)
typealias MRMediaRemoteGetNowPlayingInfoFunction = @convention(c) (DispatchQueue, @escaping ([String: Any]) -> Void) -> Void
let MRMediaRemoteGetNowPlayingInfo = unsafeBitCast(
MRMediaRemoteGetNowPlayingInfoPointer, to: MRMediaRemoteGetNowPlayingInfoFunction.self
)
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "kMRMediaRemoteNowPlayingInfoDidChangeNotification"), object: nil, queue: nil) { (notification) in
MRMediaRemoteGetNowPlayingInfo(DispatchQueue.main, { (information) in
debugPrint(information["kMRMediaRemoteNowPlayingInfoArtist"] as! String)
debugPrint(information["kMRMediaRemoteNowPlayingInfoTitle"] as! String)
debugPrint(information["kMRMediaRemoteNowPlayingInfoAlbum"] as! String)
})
}
MRMediaRemoteRegisterForNowPlayingNotifications(DispatchQueue.main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment