@@ -12,10 +12,17 @@ import AppleScriptObjC
1212import MediaPlayer
1313import SwiftyJSON
1414
15+ /// Constants relevant to the MediaService class.
16+ fileprivate struct Constants {
17+ static let player : String = " Mac "
18+ // basically the player actually playing the audio.
19+ }
1520
1621// MARK: Struct Decleration
22+
23+ // Various structs used to exchange requests through packets.
1724struct MediaInfo {
18- let player : String = " Mac "
25+ let player : String
1926 let title : String
2027 let artist : String
2128 let album : String
@@ -63,7 +70,7 @@ struct PlaybackInfo {
6370}
6471
6572struct TimelineProperties {
66- let player : String = " Mac " // to be changed
73+ let player : String
6774 let canSeek : Bool = true
6875 let pos : Int64
6976 let length : Int64
@@ -78,25 +85,19 @@ struct TimelineProperties {
7885 }
7986}
8087
81-
88+ /// Service providing the capability to control the media playing through Apple Music.
8289public class MediaService : Service {
83-
8490 // MARK: Properties
8591
86- private static let monitoringInterval : TimeInterval = 0.5
8792 private static let timeDiffTolerance : Int64 = 5
8893
89- private var monitoringTimer : Timer ? = nil
90- private var lastMediaID : String = " "
91- private var lastExternalChangeID : String = " "
92- private var lastExternalChangeDevice : Device ? = nil
9394 private var devices : [ Device ] = [ ]
9495 private var iTunesBridge : iTunesBridge ?
9596
9697 // MARK: Info
97- private var currentMediaInfo : MediaInfo = MediaInfo ( title: " title " , artist: " artist " , album: " album " , albumArtUrl: " " , url: " " , isPlaying: true )
98+ private var currentMediaInfo : MediaInfo = MediaInfo ( player : Constants . player , title: " title " , artist: " artist " , album: " album " , albumArtUrl: " " , url: " " , isPlaying: true )
9899 private var currentPlaybackInfo : PlaybackInfo = PlaybackInfo ( shuffle: false )
99- private var currentTimelineInfo : TimelineProperties = TimelineProperties ( pos: 0 , length: 10 )
100+ private var currentTimelineInfo : TimelineProperties = TimelineProperties ( player : Constants . player , pos: 0 , length: 10 )
100101
101102 @objc dynamic var pos : Int64 {
102103 get {
@@ -167,13 +168,11 @@ public class MediaService: Service {
167168
168169 public func handleDataPacket( _ dataPacket: DataPacket , fromDevice device: Device , onConnection connection: Connection ) -> Bool {
169170 guard dataPacket. isMediaRequestPacket else { return false }
170-
171-
172171 do {
173172 if try dataPacket. wantPlayerList ( ) {
174173 return sendPlayersList ( )
175174 }
176- let player = try dataPacket . getPlayerName ( )
175+
177176 if try dataPacket. wantNowPlaying ( ) {
178177 sendThemPackets ( )
179178 }
@@ -221,6 +220,7 @@ public class MediaService: Service {
221220 return false
222221 }
223222
223+ // sends all the packets with all the information relevant to the media playing
224224 private func sendThemPackets( ) {
225225 sendPacket ( DataPacket ( type: DataPacket . mediaPacketType, body: currentMediaInfo. m_dict) )
226226 sendPacket ( DataPacket ( type: DataPacket . mediaPacketType, body: currentPlaybackInfo. m_dict) )
@@ -231,7 +231,7 @@ public class MediaService: Service {
231231 Bundle . main. loadAppleScriptObjectiveCScripts ( )
232232 guard !self . devices. contains ( where: { $0. id == device. id } ) else { return }
233233 let iTunesBridgeClass : AnyClass = NSClassFromString ( " iTunesBridge " ) !
234- self . iTunesBridge = iTunesBridgeClass. alloc ( ) as! iTunesBridge
234+ self . iTunesBridge = ( iTunesBridgeClass. alloc ( ) as! iTunesBridge )
235235
236236 self . devices. append ( device)
237237
@@ -277,21 +277,18 @@ public class MediaService: Service {
277277
278278 print ( " pos is \( pos) " )
279279
280- self . currentMediaInfo = MediaInfo ( title: name, artist: artist, album: album, albumArtUrl: albumArtUrl, url: url, isPlaying: isPlaying)
281- self . currentTimelineInfo = TimelineProperties ( pos: Int64 ( pos) * 1000 , length: Int64 ( totalTime) )
280+ self . currentMediaInfo = MediaInfo ( player : Constants . player , title: name, artist: artist, album: album, albumArtUrl: albumArtUrl, url: url, isPlaying: isPlaying)
281+ self . currentTimelineInfo = TimelineProperties ( player : Constants . player , pos: Int64 ( pos) * 1000 , length: Int64 ( totalTime) )
282282
283283 sendThemPackets ( )
284+ sendVolume ( )
284285 }
285286 }
286287
287288 public func cleanup( for device: Device ) {
288289 guard let index = self . devices. index ( where: { $0. id == device. id } ) else { return }
289290
290291 self . devices. remove ( at: index)
291-
292- if self . devices. count == 0 {
293- // self.stopMonitoring()
294- }
295292 }
296293
297294 public func actions( for device: Device ) -> [ ServiceAction ] {
@@ -306,7 +303,6 @@ public class MediaService: Service {
306303 guard packet. isMediaPacket == true else { return }
307304
308305 for device in devices {
309- // device.send(DataPacket.mediaPacket(player: playerName, title: "testing", artist: "dunno", album: "test", isPlaying: true))
310306 device. send ( packet)
311307 }
312308 }
@@ -323,7 +319,7 @@ public class MediaService: Service {
323319
324320 private func sendVolume( ) {
325321 let body = [
326- " player " : " Mac " ,
322+ " player " : Constants . player ,
327323 " volume " : iTunesBridge? . soundVolume as AnyObject
328324 ] as Dictionary < String , AnyObject >
329325 let packet = DataPacket ( type: DataPacket . mediaPacketType, body: body)
@@ -350,7 +346,7 @@ public class MediaService: Service {
350346 self . currentMediaInfo. isPlaying = false
351347 sendPacket ( DataPacket ( type: DataPacket . mediaPacketType, body: currentMediaInfo. m_dict) )
352348 self . pos = 0
353- self . currentTimelineInfo = TimelineProperties ( pos: pos * 1000 , length: self . currentTimelineInfo. length)
349+ self . currentTimelineInfo = TimelineProperties ( player : Constants . player , pos: pos * 1000 , length: self . currentTimelineInfo. length)
354350 sendPacket ( DataPacket ( type: DataPacket . mediaPacketType, body: currentTimelineInfo. m_dict) )
355351 }
356352
0 commit comments