Skip to content

Instantly share code, notes, and snippets.

@cosven
Created May 11, 2022 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosven/71684e2e08ec07073d418e16cd2dba47 to your computer and use it in GitHub Desktop.
Save cosven/71684e2e08ec07073d418e16cd2dba47 to your computer and use it in GitHub Desktop.
macOS nowplaying for FeelUOwn
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