Created
May 11, 2022 08:01
-
-
Save cosven/71684e2e08ec07073d418e16cd2dba47 to your computer and use it in GitHub Desktop.
macOS nowplaying for FeelUOwn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import MediaPlayer | |
import Network | |
let cmdCenter = MPRemoteCommandCenter.shared() | |
cmdCenter.togglePlayPauseCommand.addTarget{ (e) -> MPRemoteCommandHandlerStatus in | |
return .success | |
} | |
var nowPlayingInfo = [String: Any]() | |
nowPlayingInfo[MPMediaItemPropertyTitle] = "Yes, cosven" | |
nowPlayingInfo[MPMediaItemPropertyArtist] = "cosven" | |
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo | |
MPNowPlayingInfoCenter.default().playbackState = .playing | |
let queue = DispatchQueue.global() | |
let endpoint = NWEndpoint.hostPort(host: "127.0.0.1", port: 23333) | |
let params = NWParameters(tls: nil, tcp:NWProtocolTCP.Options()) | |
let conn = NWConnection(to: endpoint, using: params) | |
// TODO: We should build a StreamReader on top of Data. | |
// TODO: Swift also has async/await support in URLSession, | |
// we should take a look at | |
// https://developer.apple.com/documentation/foundation/urlsessionstreamtask . | |
// TODO: It seems that URLSessionStreamDelegate does not support async/await | |
// because the underlying InputStream/OutputStream does not has async/await API? | |
conn.stateUpdateHandler = { state in | |
if (state == .ready) { | |
conn.receive(minimumIncompleteLength: 1, maximumLength: 256, completion: { (content, ctx, isCompleted, err) in | |
if let c = content, let s = String(data:c, encoding: .utf8){ | |
print(s) | |
} | |
}) | |
} | |
} | |
conn.start(queue: queue) | |
RunLoop.main.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment