2022 Audio & Video
WWDC22 · 26 min · Audio & Video
Explore more content with MusicKit
Discover how you can enhance and personalize your app using MusicKit. We’ll take you through the latest additions to the MusicKit framework and explore how you can bring music content to your app through requests, metadata, and more.
Watch at developer.apple.com ↗Code shown on screen · 21 snippets
Existing catalog search request
// Loading catalog search top results
var searchRequest = MusicCatalogSearchRequest(
term: "Hello",
types: [
Artist.self,
Album.self,
Song.self
]
)
let searchResponse = try await searchRequest.response()
print("\(searchResponse)") Loading catalog search top results
// Loading catalog search top results
var searchRequest = MusicCatalogSearchRequest(
term: "Hello",
types: [
Artist.self,
Album.self,
Song.self
]
)
searchRequest.includeTopResults = true
let searchResponse = try await searchRequest.response()
print("\(searchResponse.topResults)") Loading search suggestions
// Loading suggestions
let request = MusicCatalogSearchSuggestionsRequest(term: "shaz")
let response = try await request.response()
print("\(response)") Loading catalog top charts
// Loading catalog top charts.
let request = MusicCatalogChartsRequest(
kinds: [.dailyGlobalTop, .mostPlayed, .cityTop],
types: [Song.self, Playlist.self]
)
let response = try await request.response()
print("\(response.playlistCharts.first)") Loading audio variants
// Loading audio variants
let album = …
let detailedAlbum = try await album.with(.audioVariants)
print("\(detailedAlbum.debugDescription)") Showing currently playing audio variants
// Showing currently playing audio variants
var musicPlayerQueue = ApplicationMusicPlayer.shared.queue
var musicPlayerState = ApplicationMusicPlayer.shared.state
var body: some View {
if let currentEntry = musicPlayerQueue.currentEntry {
VStack {
MyPlayerEntryView(currentEntry)
if musicPlayerState.audioVariant == .dolbyAtmos {
Image("dolby-atmos-badge")
}
}
}
} Loading recently played containers
// Loading recently played containers
let request = MusicRecentlyPlayedContainerRequest()
let response = try await request.response()
print("\(response)") Loading recently played songs
// Loading recently played songs
let request = MusicRecentlyPlayedRequest<Song>()
let response = try await request.response()
print("\(response)") Loading personal recommendations and printing first recommendation
// Loading personal recommendations
let request = MusicPersonalRecommendationsRequest()
let response = try await request.response()
print("\(response.recommendations.first)") Loading personal recommendations and printing second recommendation
// Loading personal recommendations
let request = MusicPersonalRecommendationsRequest()
let response = try await request.response()
print("\(response.recommendations[1])") Loading library playlists
private func loadLibraryPlaylists() async throws {
let request = MusicLibraryRequest<Playlist>()
let response = try await request.response()
self.response = response
} Displaying library playlists
List {
Section(header: Text("Library Playlists").fontWeight(.semibold)) {
ForEach(response.items) { playlist in
PlaylistCell(playlist)
}
}
} Fetching all albums in the library
// Fetching all albums in the library
let request = MusicLibraryRequest<Album>()
let response = try await request.response()
print("\(response)") Fetching all compilations in the library
// Fetching all compilations in the library
var request = MusicLibraryRequest<Album>()
request.filter(matching: \.isCompilation, equalTo: true)
let response = try await request.response()
print("\(response)") Fetching all dance compilations in the library
// Fetching all dance compilations in the library
var request = MusicLibraryRequest<Album>()
request.filter(matching: \.isCompilation, equalTo: true)
request.filter(matching: \.genres, contains: danceGenre)
let response = try await request.response()
print("\(response)") Fetching all downloaded dance compilations in the library
// Fetching all downloaded dance compilations in the library
var request = MusicLibraryRequest<Album>()
request.filter(matching: \.isCompilation, equalTo: true)
request.filter(matching: \.genres, contains: danceGenre)
request.includeDownloadedContentOnly = true
let response = try await request.response()
print("\(response)") Fetching all albums sectioned by genre
// Fetching all albums sectioned by genre
var request = MusicLibrarySectionedRequest<Genre, Album>()
let response = try await request.response()
print("\(response)") Fetching all albums sectioned by genre sorted by artist name
// Fetching all albums sectioned by genre sorted by artist name
var request = MusicLibrarySectionedRequest<Genre, Album>()
request.sortItems(by: \.artistName, ascending: true)
let response = try await request.response()
print("\(response)") Fetching relationships using the with method without a preferred source
// Fetching relationships using the with method
let album = …
let detailedAlbum = try await album.with(.tracks)
print("\(album.tracks)") Fetching relationships using the with method and a preferred source
// Fetching relationships using the with method
let album = …
let detailedAlbum = try await album.with(.tracks, preferredSource: .library)
print("\(album.tracks)") Adding a track to a playlist
Task {
try await MusicLibrary.shared(add: selectedTrack, to: playlist)
isShowingPlaylistPicker = false
} Resources
Related sessions
-
18 min -
38 min -
18 min