I am trying to create a music player with Swift and MusicKit, but I am getting this error when trying to load up the queue with the selected album from MusicKit. I don't get why the playparameters for the album and MPMusicPlayerPlayParametersQueueDescriptor are different.
let player = MPMusicPlayerController.applicationMusicPlayer
let queue = MPMusicPlayerPlayParametersQueueDescriptor(playParametersQueue: heavyRotation[album].playParameters)
player.prepareToPlay()
player.play()
Error on let queue line:
Cannot convert value of type 'PlayParameters?' to expected argument type '[MPMusicPlayerPlayParameters]'
NEW EDIT
var requestURLComponents = URLComponents()
requestURLComponents.scheme = "https"
requestURLComponents.host = "api.music.apple.com"
requestURLComponents.path = "/v1/me/history/heavy-rotation"
requestURLComponents.queryItems = [
URLQueryItem(name: "limit", value: "5")
]
guard let url = requestURLComponents.url else { return }
let request = MusicDataRequest(urlRequest: URLRequest(url: url))
do {
let response = try await request.response()
let decodedResponse = try JSONDecoder().decode(MusicItemCollection<Album>.self, from: response.data)
heavyRotation = decodedResponse
// print(response.debugDescription)
} catch {
print("error")
print(error)
print(error.localizedDescription)
}
...
Button(action: {
Task {
guard let album = heavyRotation[album] as Album? else { return }
print(album)
let player = ApplicationMusicPlayer.shared
player.queue = [album]
try await player.prepareToPlay()
try await player.play()
}
}) {
Image(systemName: "play.fill")
.frame(width: 50, height: 50, alignment: .center)
.fontSize(30)
.foregroundColor(.white)
}
While MusicKit's
PlayParametersis a structure and an opaque object,MPMusicPlayerPlayParametersinitializers expect a[String: Any]dictionary. You'll have to encode and decode thePlayParameterstoMPMusicPlayerParameters.Here's your example:
If you're using MusicKit for Swift and its music player, you can directly set the album to the queue like this:
Update - If you're trying to play an album from the library, then I had problems with that as well. As a workaround, you can get the album's local ID and then make another request to the catalog. If there's an album on Apple Music, then it should work.
Here's an example that works fine for me: