Skip to content

Instantly share code, notes, and snippets.

@d1y
Forked from Hamuko/nowPlayingPlayground.swift
Created May 5, 2021 09:33
Show Gist options
  • Save d1y/e3b032c21716ed7b501e9d1788832b04 to your computer and use it in GitHub Desktop.
Save d1y/e3b032c21716ed7b501e9d1788832b04 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