View in English

  • Apple 开发者
    • 入门汇总

    探索“入门汇总”

    • 概览
    • 学习
    • Apple Developer Program

    及时了解最新动态

    • 最新动态
    • 开发者你好
    • 平台

    探索“平台”

    • Apple 平台
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    • App Store

    精选

    • 设计
    • 分发
    • 游戏
    • 配件
    • 网页
    • Home
    • CarPlay 车载
    • 技术

    探索“技术”

    • 概览
    • Xcode
    • Swift
    • SwiftUI

    精选

    • 辅助功能
    • App Intents
    • Apple 智能
    • 游戏
    • 机器学习与 AI
    • 安全性
    • Xcode Cloud
    • 社区

    探索“社区”

    • 概览
    • “与 Apple 会面交流”活动
    • 社区主导的活动
    • 开发者论坛
    • 开源

    精选

    • WWDC
    • Swift Student Challenge
    • 开发者故事
    • App Store 大奖
    • Apple 设计大奖
    • Apple Developer Centers
    • 文档

    探索“文档”

    • 文档库
    • 技术概述
    • 示例代码
    • 《人机界面指南》
    • 视频

    发布说明

    • 精选更新
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • Apple tvOS
    • Xcode
    • 下载

    探索“下载”

    • 所有下载
    • 操作系统
    • 应用程序
    • 设计资源

    精选

    • Xcode
    • TestFlight
    • 字体
    • SF Symbols
    • Icon Composer
    • 支持

    探索“支持”

    • 概览
    • 帮助指南
    • 开发者论坛
    • “反馈助理”
    • 联系我们

    精选

    • 《开发者账户帮助》
    • 《App 审核指南》
    • 《App Store Connect 帮助》
    • 即将实行的要求
    • 协议和准则
    • 系统状态
  • 快速链接

    • 活动
    • 新闻
    • 论坛
    • 示例代码
    • 视频
 

视频

打开菜单 关闭菜单
  • 专题
  • 所有视频
  • 关于

更多视频

  • 简介
  • 概要
  • 代码
  • 将 MusicKit 整合到你的 App 中

    使用 MusicKit 将 Apple Music 的强大功能融入你的 App。我们将介绍授权、订阅状态检查、音乐选择、播放控制以及跨平台歌曲共享。了解如何使用新的音乐选择器,方便用户浏览 Apple Music 目录和自己的个人音乐库。我们还将详细讲解 SystemMusicPlayer 和 ApplicationMusicPlayer 之间的区别,并展示如何观察播放状态。

    章节

    • 0:00 - Introduction
    • 2:11 - Project setup and authorization
    • 7:10 - Music items and music picker
    • 10:54 - Music players and playback
    • 16:26 - Catalog requests
    • 20:11 - Next steps

    资源

    • Integrating MusicKit into your app
    • Apple Services Performance Partner Program
    • MusicKit
      • 高清视频
      • 标清视频

    相关视频

    WWDC23

    • 探索 SwiftUI 中的观察

    WWDC22

    • 借助 MusicKit 探索更多内容
    • Apple Music API 和 MusicKit 简介
  • 搜索此视频…
    • 4:47 - Presents the Apple Music subscription offer

      @State var showSubscriptionOffer = false
      
      let options = MusicSubscriptionOffer.Options(
          messageIdentifier: .playMusic
      )
      
      @ViewBuilder
      var musicSubsriptionButton: some View {
          Button("Subscribe to Apple Music", systemImage: "music.note") {
              showSubscriptionOffer = true
          }
          .musicSubscriptionOffer(isPresented: $showSubscriptionOffer, options: options)
      }
    • 5:59 - Adds subscription button to main view

      @State var subscription: MusicSubscription?
      
      var body: some View {
        	VStack {
              // ...
              if let subscription, subscription.canBecomeSubscriber {
                  musicSubscriptionButton
              }
          }
          .task(id: isAuthorized) {
      	      self.subscription = try? await MusicSubscription.current
              for await subscription in MusicSubscription.subscriptionUpdates {
                  self.subscription = subscription
              }
          }
      }
    • 8:48 - Add .musicPicker() modifier

      @State var showMusicPicker = false
      @State var selectedSong: Song? = nil
      
      @ViewBuilder
      var musicPickerButton: some View {
          Button("Pick some Music", systemImage: "music.note.list") {
              showMusicPicker = true
          }
          .musicPicker(isPresented: $showMusicPicker, selection: $selectedSong)
      }
      
      var body: some View {
          VStack {
              if let subscription, subscription.canBecomeSubscriber {
                  musicSubscriptionButton
              }
              musicPickerButton
          }
      }
    • 14:49 - Artwork

      @State var queue = ApplicationMusicPlayer.shared.queue
      
      var body: some View {
          VStack {
              if let artwork = queue.currentEntry?.artwork {
                  ArtworkImage(artwork, width: 200, height: 200)
              } else {
                  // Placeholder artwork
                  RoundedRectangle(cornerRadius: 16)
                      .fill(.quaternary)
                      .frame(width: 200, height: 200)
              }
          }
      }
    • 15:06 - Current entry info

      @State var queue = ApplicationMusicPlayer.shared.queue
      
      var body: some View {
          VStack {
              // ...
              if let currentSong = queue.currentEntry {
                  Text(currentSong.title)
                      .font(.title3.bold())
                    
                  if let subtitle = currentSong.subtitle {
                      Text(subtitle)
                          .font(.subheadline)
                          .foregroundStyle(.secondary)
                  }
              }
          }
      }
    • 15:14 - Playback controls (play, pause)

      let player = ApplicationMusicPlayer.shared
      @State var state = ApplicationMusicPlayer.shared.state
      
      var isPlaying: Bool {
          state.playbackStatus == .playing
      }
      
      var playPause: some View {
          Button (
              isPlaying ? "Pause": "Play",
              systemImage: isplaying ? "pause.fill" : "play.fill"
          ) {
              if isPlaying {
                  player.pause()
              } else {
                  Task {
                      try await player.play()
                  }
              }
          }
      }
    • 15:38 - Playback controls (next, previous)

      let player = ApplicationMusicPlayer.shared
      
      var controls: some View {
          HStack {
              Button("Back", systemImage: "backward.fill") {
                  Task {
                      try await player.skipToPreviousEntry()
                  }
              }
              // ...
              Button("Next", systemImage: "forward.fill") {
                  Task {
                      try await player.skipToNextEntry()
                  }
              }
          }
      }
    • 18:58 - Music catalog resource request

      func fetchSongs(songIDs: [MusicItemID]) async throws -> (featured: Song?, other: [Song]) {
          var request = MusicCatalogResourceRequest‹Song>(matching: \.id, memberOf: songIDs)
          request.options = [.findEquivalents]
          
          let response = try await request.response()
          
          let featuredSongID = songIDs[0]
          let featuredSong = response.item(for: featuredSongID)
          
          let others: [Song] = songIDs[1...].compactMap { songID in
              return response.item(for: songID)
          }
          
          return (featuredSong, others)
      }
    • 0:00 - Introduction
    • An introduction to MusicKit and an overview of how to build a music-enhanced workout app using Swift concurrency and SwiftUI.

    • 2:11 - Project setup and authorization
    • Learn how to configure your Xcode project with the necessary capabilities, request music library authorization, and present Apple Music subscription offers to users.

    • 7:10 - Music items and music picker
    • Explore the properties and relationships of MusicKit music items, and use the music picker view modifier to let users browse and select songs from the Apple Music catalog or their own library.

    • 10:54 - Music players and playback
    • Dive into using SystemMusicPlayer and ApplicationMusicPlayer. Discover how to set up playback queues, observe playback state, and build UI controls for playback in SwiftUI.

    • 16:26 - Catalog requests
    • Use structured catalog requests like MusicCatalogResourceRequest to fetch curated Apple Music content, and learn how to handle localization and content equivalency.

    • 20:11 - Next steps
    • A quick recap of MusicKit capabilities and pointers to related sessions for further learning.

Developer Footer

  • 视频
  • WWDC26
  • 将 MusicKit 整合到你的 App 中
  • 打开菜单 关闭菜单
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    打开菜单 关闭菜单
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • SF Symbols
    打开菜单 关闭菜单
    • 辅助功能
    • 配件
    • Apple 智能
    • App 扩展
    • App Store
    • 音频与视频 (英文)
    • 增强现实
    • 设计
    • 分发
    • 教育
    • 字体 (英文)
    • 游戏
    • 健康与健身
    • App 内购买项目
    • 本地化
    • 地图与位置
    • 机器学习与 AI
    • 开源资源 (英文)
    • 安全性
    • Safari 浏览器与网页 (英文)
    打开菜单 关闭菜单
    • 完整文档 (英文)
    • 部分主题文档 (简体中文)
    • 教程
    • 下载
    • 论坛 (英文)
    • 视频
    打开菜单 关闭菜单
    • 支持文档
    • 联系我们
    • 错误报告
    • 系统状态 (英文)
    打开菜单 关闭菜单
    • Apple 开发者
    • App Store Connect
    • 证书、标识符和描述文件 (英文)
    • 反馈助理
    打开菜单 关闭菜单
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program (英文)
    • Mini Apps Partner Program
    • News Partner Program (英文)
    • Video Partner Program (英文)
    • 安全赏金计划 (英文)
    • Security Research Device Program (英文)
    打开菜单 关闭菜单
    • 与 Apple 会面交流
    • Apple Developer Center
    • App Store 大奖 (英文)
    • Apple 设计大奖
    • Apple Developer Academies (英文)
    • WWDC
    阅读最近新闻。
    获取 Apple Developer App。
    版权所有 © 2026 Apple Inc. 保留所有权利。
    使用条款 隐私政策 协议和准则