Created
April 8, 2022 13:42
-
-
Save cosven/0da4242b3629113dbfae6fb8685085c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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