Friday, March 8, 2024
HomeiOS DevelopmentHow you can get notified when iOS music app modifications state

How you can get notified when iOS music app modifications state


I’ve tried each doable technique to subscribe and I am not getting any notifications

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: .foremost) { [weak self] notification in
        debugPrint("hey")
    }
    
    NotificationCenter.default.addObserver(forName: .MPMusicPlayerControllerPlaybackStateDidChange, object: appMusicPlayer, queue: .foremost) { [weak self] notification in
        debugPrint("hey")
    }
    
    NotificationCenter.default.addObserver(forName: .MPMusicPlayerControllerPlaybackStateDidChange, object: queuePlayer, queue: .foremost) { [weak self] notification in
        debugPrint("hey")
    }

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

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

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

}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments