1+ import Foundation
2+ import MediaPlayer
3+ import AVFoundation
4+
5+ // Tell Swift this function exists somewhere at runtime
6+ @_silgen_name ( " rust_hello " )
7+ public func rustHello( )
8+
9+ @_cdecl( " hello_world" )
10+ public func startNowPlayingSession( ) {
11+ // Get the shared MPNowPlayingInfoCenter
12+ let nowPlayingInfoCenter = MPNowPlayingInfoCenter . default ( )
13+
14+ // Set up remote command targets BEFORE setting the info
15+ setupRemoteCommandTargets ( )
16+
17+ // Create now playing info dictionary
18+ var nowPlayingInfo = [ String: Any] ( )
19+
20+ // Set basic metadata
21+ nowPlayingInfo [ MPMediaItemPropertyTitle] = " Your Song Title "
22+ nowPlayingInfo [ MPMediaItemPropertyArtist] = " Your Artist Name "
23+ nowPlayingInfo [ MPMediaItemPropertyAlbumTitle] = " Your Album Name "
24+ nowPlayingInfo [ MPMediaItemPropertyPlaybackDuration] = 180.0
25+ nowPlayingInfo [ MPNowPlayingInfoPropertyElapsedPlaybackTime] = 0.0
26+ // This is crucial - set to 1.0 to indicate "playing" state
27+ nowPlayingInfo [ MPNowPlayingInfoPropertyPlaybackRate] = 1.0
28+ // Add media type
29+ nowPlayingInfo [ MPNowPlayingInfoPropertyMediaType] = MPNowPlayingInfoMediaType . audio. rawValue
30+
31+ // Set the now playing info
32+ nowPlayingInfoCenter. nowPlayingInfo = nowPlayingInfo
33+
34+ print ( " Now Playing session started " )
35+ print ( " NowPlaying info set: \( nowPlayingInfo) " )
36+
37+ // Keep the process alive so the Now Playing info persists
38+ print ( " Keeping process alive... Press Ctrl+C to exit " )
39+ RunLoop . main. run ( )
40+ }
41+
42+ private func setupRemoteCommandTargets( ) {
43+ let commandCenter = MPRemoteCommandCenter . shared ( )
44+
45+ // Play command
46+ commandCenter. playCommand. addTarget { event in
47+ // Handle play action
48+ print ( " Play command received " )
49+ return . success
50+ }
51+
52+ // Pause command
53+ commandCenter. pauseCommand. addTarget { event in
54+ // Handle pause action
55+ print ( " Pause command received " )
56+ return . success
57+ }
58+
59+ // Next track command
60+ commandCenter. nextTrackCommand. addTarget { event in
61+ // Handle next track action
62+ print ( " Next track command received " )
63+ return . success
64+ }
65+
66+ // Previous track command
67+ commandCenter. previousTrackCommand. addTarget { event in
68+ // Handle previous track action
69+ print ( " Previous track command received " )
70+ return . success
71+ }
72+ }
0 commit comments