11import Foundation
22import MediaPlayer
33import AVFoundation
4+ import AppKit
5+ import Cocoa
46
57@_silgen_name ( " rust_resume_playback_command " )
68public func rustStartPlaybackCommand( )
@@ -16,73 +18,121 @@ public func rustPreviousTrackCommand()
1618
1719@_cdecl ( " swift_set_metadata_title " )
1820public func setMetadataTitle( title : UnsafePointer < CChar > ) {
19- let s = String ( cString: title)
20- var nowPlayingInfo = [ String: Any] ( )
21- nowPlayingInfo [ MPMediaItemPropertyTitle] = s
22- MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
21+ DispatchQueue . main. async {
22+ let s = String ( cString: title)
23+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
24+ nowPlayingInfo [ MPMediaItemPropertyTitle] = s
25+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
26+ }
2327}
2428
2529@_cdecl ( " swift_set_metadata_artist " )
2630public func setMetadataArtist( artist : UnsafePointer < CChar > ) {
27- let s = String ( cString: artist)
28- var nowPlayingInfo = [ String: Any] ( )
29- nowPlayingInfo [ MPMediaItemPropertyArtist] = s
30- MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
31+ DispatchQueue . main. async {
32+ let s = String ( cString: artist)
33+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
34+ nowPlayingInfo [ MPMediaItemPropertyArtist] = s
35+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
36+ }
3137}
3238
3339@_cdecl ( " swift_set_metadata_album " )
3440public func setMetadataAlbumTitle( album : UnsafePointer < CChar > ) {
35- let s = String ( cString: album)
36- var nowPlayingInfo = [ String: Any] ( )
37- nowPlayingInfo [ MPMediaItemPropertyAlbumTitle] = s
38- MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
41+ DispatchQueue . main. async {
42+ let s = String ( cString: album)
43+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
44+ nowPlayingInfo [ MPMediaItemPropertyAlbumTitle] = s
45+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
46+ }
3947}
4048
4149@_cdecl ( " swift_set_metadata_genre " )
4250public func setMetadataGenre( genre : UnsafePointer < CChar > ) {
43- let s = String ( cString: genre)
44- var nowPlayingInfo = [ String: Any] ( )
45- nowPlayingInfo [ MPMediaItemPropertyGenre] = s
46- MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
51+ DispatchQueue . main. async {
52+ let s = String ( cString: genre)
53+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
54+ nowPlayingInfo [ MPMediaItemPropertyGenre] = s
55+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
56+ }
57+ }
58+
59+ func resize( image: NSImage , to newSize: NSSize ) -> NSImage {
60+ let resizedImage = NSImage ( size: newSize)
61+ resizedImage. lockFocus ( )
62+ defer { resizedImage. unlockFocus ( ) }
63+
64+ image. draw (
65+ in: NSRect ( origin: . zero, size: newSize) ,
66+ from: NSRect ( origin: . zero, size: image. size) ,
67+ operation: . copy,
68+ fraction: 1.0
69+ )
70+
71+ return resizedImage
72+ }
73+
74+ @_cdecl ( " swift_set_metadata_image " )
75+ public func setMetadataImage( bytes: UnsafePointer < UInt8 > , length: Int ) {
76+ DispatchQueue . main. async {
77+ let data = Data ( bytes: bytes, count: length)
78+ guard let image = NSImage ( data: data) else {
79+ print ( " Failed to convert data to NSImage " )
80+ return
81+ }
82+ let scaledImg = resize ( image: image, to: NSSize ( width: 512 , height: 512 ) )
83+ let artwork = MPMediaItemArtwork ( boundsSize: image. size) { _ in
84+ return scaledImg
85+ }
86+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
87+ nowPlayingInfo [ MPMediaItemPropertyArtwork] = artwork
88+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
89+ }
4790}
4891
4992@_cdecl ( " swift_set_metadata_media_type " )
5093public func setMetadataMediaType( id: Int ) {
51- var nowPlayingInfo = [ String: Any] ( )
52- if ( id == 0 ) {
53- nowPlayingInfo [ MPNowPlayingInfoPropertyMediaType] = MPNowPlayingInfoMediaType . audio. rawValue
54- } else {
55- nowPlayingInfo [ MPNowPlayingInfoPropertyMediaType] = MPNowPlayingInfoMediaType . video. rawValue
94+ DispatchQueue . main. async {
95+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
96+ if ( id == 0 ) {
97+ nowPlayingInfo [ MPNowPlayingInfoPropertyMediaType] = MPNowPlayingInfoMediaType . audio. rawValue
98+ } else {
99+ nowPlayingInfo [ MPNowPlayingInfoPropertyMediaType] = MPNowPlayingInfoMediaType . video. rawValue
100+ }
101+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
56102 }
57- MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
58103}
59104
60105@_cdecl ( " swift_set_playback_duration " )
61106public func setPlaybackDuration( seconds : Double ) {
62- var nowPlayingInfo = [ String: Any] ( )
63- nowPlayingInfo [ MPMediaItemPropertyPlaybackDuration] = seconds
64- MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
107+ DispatchQueue . main. async {
108+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
109+ nowPlayingInfo [ MPMediaItemPropertyPlaybackDuration] = seconds
110+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
111+ }
65112}
66113
67114@_cdecl ( " swift_set_elapsed_duration " )
68115public func setElapsedPlaybackTime( seconds : Double ) {
69- var nowPlayingInfo = [ String: Any] ( )
70- nowPlayingInfo [ MPNowPlayingInfoPropertyElapsedPlaybackTime] = seconds
71- MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
116+ DispatchQueue . main. async {
117+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
118+ nowPlayingInfo [ MPNowPlayingInfoPropertyElapsedPlaybackTime] = seconds
119+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
120+ }
72121}
73122
74123@_cdecl ( " swift_set_playback_rate " )
75124public func setPlaybackRate( rate : Double ) {
76- var nowPlayingInfo = [ String: Any] ( )
77- nowPlayingInfo [ MPNowPlayingInfoPropertyPlaybackRate] = rate
78- MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
125+ DispatchQueue . main. async {
126+ var nowPlayingInfo = MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo ?? [ String: Any] ( )
127+ nowPlayingInfo [ MPNowPlayingInfoPropertyPlaybackRate] = rate
128+ MPNowPlayingInfoCenter . default ( ) . nowPlayingInfo = nowPlayingInfo
129+ }
79130}
80131
81132@_cdecl ( " swift_start_session " )
82133public func startSession( ) {
83134 setupRemoteCommandTargets ( ) // Create event hooks for media session.
84- setPlaybackRate ( rate: 0.0 ) // Ensures NowPlaying is properly displayed.
85- RunLoop . main. run ( ) // Loop the thread indefinitely; until killed.
135+ RunLoop . main. run ( ) // Loop the thread indefinitely until killed.
86136}
87137
88138private func setupRemoteCommandTargets( ) {
0 commit comments