Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

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 ↗

Transcript all transcripts

Code shown on screen · 21 snippets

Existing catalog search request swift · at 4:20 ↗
// 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 swift · at 4:44 ↗
// 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 swift · at 5:09 ↗
// Loading suggestions

let request = MusicCatalogSearchSuggestionsRequest(term: "shaz")
let response = try await request.response()
print("\(response)")
Loading catalog top charts swift · at 6:30 ↗
// 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 swift · at 8:10 ↗
// Loading audio variants

let album = 
let detailedAlbum = try await album.with(.audioVariants)
print("\(detailedAlbum.debugDescription)")
Showing currently playing audio variants swift · at 9:09 ↗
// Showing currently playing audio variants

@ObservedObject var musicPlayerQueue = ApplicationMusicPlayer.shared.queue
@ObservedObject 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 swift · at 10:28 ↗
// Loading recently played containers

let request = MusicRecentlyPlayedContainerRequest()
let response = try await request.response()
print("\(response)")
Loading recently played songs swift · at 10:41 ↗
// Loading recently played songs

let request = MusicRecentlyPlayedRequest<Song>()
let response = try await request.response()
print("\(response)")
Loading personal recommendations and printing first recommendation swift · at 11:21 ↗
// Loading personal recommendations

let request = MusicPersonalRecommendationsRequest()
let response = try await request.response()
print("\(response.recommendations.first)")
Loading personal recommendations and printing second recommendation swift · at 11:51 ↗
// Loading personal recommendations

let request = MusicPersonalRecommendationsRequest()
let response = try await request.response()
print("\(response.recommendations[1])")
Loading library playlists swift · at 13:36 ↗
@MainActor
private func loadLibraryPlaylists() async throws {
  let request = MusicLibraryRequest<Playlist>()
  let response = try await request.response()
  self.response = response
}
Displaying library playlists swift · at 14:23 ↗
List {
  Section(header: Text("Library Playlists").fontWeight(.semibold)) {
    ForEach(response.items) { playlist in
      PlaylistCell(playlist)
    }
  }
}
Fetching all albums in the library swift · at 15:47 ↗
// Fetching all albums in the library

let request = MusicLibraryRequest<Album>()

let response = try await request.response()
print("\(response)")
Fetching all compilations in the library swift · at 16:38 ↗
// 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 swift · at 17:08 ↗
// 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 swift · at 17:29 ↗
// 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 swift · at 18:29 ↗
// 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 swift · at 19:04 ↗
// 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 swift · at 20:58 ↗
// 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 swift · at 21:11 ↗
// 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 swift · at 22:09 ↗
Task { 
  try await MusicLibrary.shared(add: selectedTrack, to: playlist)
  isShowingPlaylistPicker = false
}

Resources