Reputation: 804
I'm having problems with finding out information or a documentation on how to open the music app with a specific album and playing it.
The closest I found right now is
let url = URL(string: "music://")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
I've tried a few combinations with the path and options but no luck, it always just opens the app and does nothing beyond that.
I initially thought this would open through the MPMediaPlayer but that just plays it in the app and is now what I need.
Upvotes: 0
Views: 1714
Reputation: 563
For MPMediaQuery
For Apple Music and MusicKit
https://developer.apple.com/documentation/MusicKit/
As of June 2024, if the MusicKit song is an iTunes song, you cannot use the URL. You need to decode the playParameters of the response object to find the musicKit_persistentID and then pass that into an MPMediaQuery for use in the MPMusicPlayerController.systemMusicPlayer. The musicKit_persistentID maps to persistentID on MPMediaItem:
https://developer.apple.com/documentation/mediaplayer/mpmediaitem/1621766-persistentid
If the song is an Apple Music song, requiring a subscription, sometimes using the response ID works, sometimes it does not. I don’t have a clear answer for Apple Music subscription songs as each data set for each Apple Music module contains different class types. Playlists work differently than Library requests.
The code below assumes you are using a MusicKit song:
https://developer.apple.com/documentation/musickit/song
https://developer.apple.com/documentation/musickit/song/playparameters
Here's some pseudo code that helps illustrate the issue:
let data = try JSONEncoder().encode(
song.playParamaters
)
let playParameters = try JSONDecoder().decode(
MPMusicPlayerPlayParameters.self,
from: data
)
let queue = MPMusicPlayerPlayParametersQueueDescriptor(
playParametersQueue: [
playParameters
]
)
if let catalogId = playParameters.dictionary["musicKit_persistentID"] as? String {
var pID = Int64(catalogId) ?? 0
var persistentID : UInt64 = 0
if pID < 0 {
persistentID = UInt64(bitPattern: Int64(pID))
} else {
persistentID = UInt64(pID)
}
// MPMediaQuery
let everything : MPMediaQuery = MPMediaQuery.init()
let predicate = MPMediaPropertyPredicate.init(
value: persistentID,
forProperty: MPMediaItemPropertyPersistentID
)
everything.addFilterPredicate(predicate)
if let collection = everything.collections?.first {
if collection.items.first?.isCloudItem ?? false {
// Apple Music
DispatchQueue.main.async {
MPMusicPlayerController.systemMusicPlayer.setQueue(
with: [song.id]
)
MPMusicPlayerController.systemMusicPlayer.nowPlayingItem = collection.items.first
MPMusicPlayerController.systemMusicPlayer.prepareToPlay()
MPMusicPlayerController.systemMusicPlayer.currentPlaybackTime = 0
MPMusicPlayerController.systemMusicPlayer.play()
}
} else {
// downloaded iTunes Purchase
DispatchQueue.main.async {
MPMusicPlayerController.systemMusicPlayer.setQueue(
with: collection
)
MPMusicPlayerController.systemMusicPlayer.nowPlayingItem = collection.items.first!
MPMusicPlayerController.systemMusicPlayer.prepareToPlay()
MPMusicPlayerController.systemMusicPlayer.currentPlaybackTime = 0
MPMusicPlayerController.systemMusicPlayer.play()
}
}
}
}
Upvotes: 0
Reputation: 534895
You want the MPMusicPlayerController systemMusicPlayer
.
https://developer.apple.com/documentation/mediaplayer/mpmusicplayercontroller
It is the Music app.
Upvotes: 2