iOS 音乐应用更改状态时如何收到通知

问题描述 投票:0回答:1

我已经尝试了所有可能的订阅方法,但没有收到任何通知

let musicPlayer = MPMusicPlayerController.systemMusicPlayer
let appMusicPlayer = MPMusicPlayerController.applicationMusicPlayer
let queuePlayer = MPMusicPlayerController.applicationQueuePlayer

func subscribe() {


    MPMediaLibrary.requestAuthorization { _ in }      

    musicPlayer.beginGeneratingPlaybackNotifications()
    appMusicPlayer.beginGeneratingPlaybackNotifications()
    queuePlayer.beginGeneratingPlaybackNotifications()

    NotificationCenter.default.addObserver(forName: .MPMusicPlayerControllerPlaybackStateDidChange, object: musicPlayer, queue: .main) { [weak self] notification in
        debugPrint("hello")
    }
    
    NotificationCenter.default.addObserver(forName: .MPMusicPlayerControllerPlaybackStateDidChange, object: appMusicPlayer, queue: .main) { [weak self] notification in
        debugPrint("hello")
    }
    
    NotificationCenter.default.addObserver(forName: .MPMusicPlayerControllerPlaybackStateDidChange, object: queuePlayer, queue: .main) { [weak self] notification in
        debugPrint("hello")
    }

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(playbackStateChanged(_:)),
        name: .MPMusicPlayerControllerPlaybackStateDidChange,
        object: musicPlayer
    )

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(playbackStateChanged(_:)),
        name: .MPMusicPlayerControllerPlaybackStateDidChange,
        object: appMusicPlayer
    )

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(playbackStateChanged(_:)),
        name: .MPMusicPlayerControllerPlaybackStateDidChange,
        object: queuePlayer
    )

}

ios swift nsnotificationcenter mpmediaplayercontroller
1个回答
0
投票
  • 将“NSAppleMusicUsageDescription”添加到您的 .plist 文件中并包含字符串值 这描述了为什么您需要访问媒体播放器框架。

  • 使用示例代码

    
    
    import SwiftUI
    import MediaPlayer
    
    struct ContentView: View {
        var body: some View {
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundStyle(.tint)
                Text("SetupMusic").onTapGesture {
                    setup()
                }
            }
            .padding()
        }
    }
    
    private func setup() {
        debugPrint("Rquesting access")
        MPMediaLibrary.requestAuthorization { permission in
            debugPrint("Setting up music player")
            let musicPlayer = MPMusicPlayerController.systemMusicPlayer
            musicPlayer.beginGeneratingPlaybackNotifications()
            musicPlayer.beginGeneratingPlaybackNotifications()
            debugPrint("Intial State: \(musicPlayer.playbackState)")
            NotificationCenter.default.addObserver(forName: .MPMusicPlayerControllerPlaybackStateDidChange, object: musicPlayer, queue: .main) {  notification in
                debugPrint("State changed to \(musicPlayer.playbackState)")
            }
        }
    }
    
    #Preview {
        ContentView()
    }
    
    
    
  1. 在真实的 iOS 设备上运行应用程序。模拟器不支持MPMediaPlayer 框架。
  2. 点击按钮以允许许可。
  3. 一旦获得授权,您应该获得初始状态。 (停止/暂停)
  4. 转到系统音乐应用程序并在收音机上播放一些内容。
  5. 再次启动应用程序,您应该看到状态更改为“正在播放”
  6. 向下滑动查看通知并暂停音乐,状态将更改为 已暂停
  7. 仅当应用程序处于前台状态时才会触发事件。
© www.soinside.com 2019 - 2024. All rights reserved.