I tried refreshing before playing, but same thing.
public func playTrack(_ track: Track) async {
// Check for valid PlayParameters
guard track.playParameters != nil else {
print("Error: Track (track.title) is missing PlayParameters. Cannot play.")
return
}
let status = await MusicAuthorization.request()
if status != .authorized {
print("Music authorization not granted.")
return
}
do {
// **Fetch updated track information if needed**
let updatedTrack: Track
if let appleTrack = AppleTrack(from: track) {
if let refreshedTrack = await fetchTrackDetails(for: appleTrack.title) {
print("Fetched updated track details for \(refreshedTrack.title).")
updatedTrack = refreshedTrack
} else {
print("Failed to fetch updated track details. Proceeding with existing track.")
updatedTrack = track
}
} else {
updatedTrack = track
}
// Insert the track into the queue
try await player.queue.insert(updatedTrack, position: .afterCurrentEntry)
print("Queued track: \(updatedTrack.title) with PlayParameters present.")
print("Play Parameters:", updatedTrack.playParameters ?? "None")
// Log the queue state
if player.queue.entries.isEmpty {
print("Queue is still empty after inserting track \(updatedTrack.title).")
return
}
print("Queue contains \(player.queue.entries.count) entries.")
// Notify listeners of track info
self.notifyAppleMusicTrackInfo(updatedTrack)
// Prepare and play
try await player.prepareToPlay()
try await player.play()
print("Playback started for track: \(updatedTrack.title)")
} catch {
print("Error during playback for \(track.title): \(error.localizedDescription)")
}
}
private func fetchTrackDetails(for id: String) async -> Track? {
do {
// Create a search request for the specific track ID
let searchRequest = MusicCatalogSearchRequest(term: id, types: [Song.self]) // Use Song.self
let response = try await searchRequest.response()
// Retrieve the first matching song and convert it to a Track
if let song = response.songs.first {
print("Fetched song: \(song.title) by \(song.artistName)")
return .song(song) // Wrap the Song as a Track
} else {
print("No track found for ID \(id)")
return nil
}
} catch {
print("Error fetching track details for ID \(id): \(error.localizedDescription)")
return nil
}
}
Post
Replies
Boosts
Views
Activity
I'm in Puerto Rico right now, is that a problem with Apple Music API?? applicationQueuePlayer _establishConnectionIfNeeded timeout [ping did not pong]
Queue cleared.
Apple Music player reset successfully.
Queued track: Uncle Salty with PlayParameters present.
Play Parameters: PlayParameters(id: 1660109276, kind: "song", isLibrary: nil, catalogID: nil, libraryID: nil, deviceLocalID: nil, rawValues: [:])
Queue contains 1 entries.
prepareToPlay failed [no target descriptor]
Error during playback for Uncle Salty: The operation couldn’t be completed. (MPMusicPlayerControllerErrorDomain error 1.)
Notification BASS DSD NSConcreteNotification 0x6000032f1fc0 {name = kUpdateSongInfo; object = {
AlbumTitle = "Toys In the Attic";
ArtistName = Aerosmith;
SongArtwork = "<NSImage 0x6000088d0320 Size={300, 300} RepProvider=<NSImageArrayRepProvider: 0x600003b24b00, reps:(\n "NSBitmapImageRep 0x6000011c0460 Size={300, 300} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=300x300 Alpha=NO Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x6000032f4380"\n)>>";
SongLength = "249.754";
SongTitle = "Uncle Salty";
Source = AppleMusic;
}}
Apple Music track loaded: Uncle Salty by Aerosmith
Using your suggestion, the queue won't even accept the entry: Queue cleared.
Apple Music player reset successfully.
Track Details:
Title: Adam's Apple
Artist: Aerosmith
PlayParameters: PlayParameters(id: 1660109281, kind: "song", isLibrary: nil, catalogID: nil, libraryID: nil, deviceLocalID: nil, rawValues: [:])
Queue initialized with track: Adam's Apple
Queue is empty after adding track Adam's Apple.
public func playTrack(_ track: Track) async {
guard let playParameters = track.playParameters else {
print("Error: Track \(track.title) is missing PlayParameters. Cannot play.")
return
}
print("Track Details:")
print(" - Title: \(track.title)")
print(" - Artist: \(track.artistName)")
print(" - PlayParameters: \(playParameters)")
// Request Music Authorization
let status = await MusicAuthorization.request()
if status != .authorized {
print("Music authorization not granted.")
return
}
do {
// Replace the queue with the track
player.queue = [track]
print("Queue initialized with track: \(track.title)")
// Validate the queue state
if player.queue.entries.isEmpty {
print("Queue is empty after adding track \(track.title).")
return
}
// Log queue details
print("Queue contains \(player.queue.entries.count) entries.")
for entry in player.queue.entries {
print("Queue Entry: \(entry.title)")
}
// Notify about track info
self.notifyAppleMusicTrackInfo(track)
// Prepare and play
try await player.prepareToPlay()
try await player.play()
print("Playback started for track: \(track.title)")
} catch {
print("Error during playback for \(track.title): \(error.localizedDescription)")
}
}
Queue cleared.
Apple Music player reset successfully.
Queued track: Uncle Salty with PlayParameters present.
Play Parameters: PlayParameters(id: 1660109276, kind: "song", isLibrary: nil, catalogID: nil, libraryID: nil, deviceLocalID: nil, rawValues: [:])
Queue contains 1 entries.
prepareToPlay failed [no target descriptor]
Error during playback for Uncle Salty: The operation couldn’t be completed. (MPMusicPlayerControllerErrorDomain error 1.)
Notification BASS DSD NSConcreteNotification 0x600003cd70e0 {name = kUpdateSongInfo; object = {
AlbumTitle = "Toys In the Attic";
ArtistName = Aerosmith;
SongArtwork = "<NSImage 0x60000873dd60 Size={300, 300} RepProvider=<NSImageArrayRepProvider: 0x6000034711c0, reps:(\n "NSBitmapImageRep 0x600001eace00 Size={300, 300} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=300x300 Alpha=NO Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x600003cb0040"\n)>>";
SongLength = "249.754";
SongTitle = "Uncle Salty";
Source = AppleMusic;
}}
Changes to the code: private let player = ApplicationMusicPlayer.shared
// MARK: - Play Single Track
public func playTrack(_ track: Track) async {
guard let playParameters = track.playParameters else {
print("Error: Track \(track.title) is missing PlayParameters. Cannot play.")
return
}
let status = await MusicAuthorization.request()
if status != .authorized {
print("Music authorization not granted.")
}
do {
// Insert the track into the queue
try await player.queue.insert(track, position: .afterCurrentEntry)
print("Queued track: \(track.title) with PlayParameters present.")
print("Play Parameters:", track.playParameters ?? "None")
// Log the queue state
if player.queue.entries.isEmpty {
print("Queue is still empty after inserting track \(track.title).")
return
}
print("Queue contains \(player.queue.entries.count) entries.")
self.notifyAppleMusicTrackInfo(track)
// Prepare and play
try await player.prepareToPlay()
try await player.play()
print("Playback started for track: \(track.title)")
} catch {
print("Error during playback for \(track.title): \(error.localizedDescription)")
}
}
APPLE SUCKS! THAT'S WHAT IT IS! WTF!!!!! why do we have to deal with this crap???? Why can't they manage their own crap?? and let us code!!!@
******* LOOSERS!!