MPMusicPlayerController responds slowly when systemMusicPlayer is fast when application

I have a music application that uses MPMusicPlayerController.

I originally wrote it using the systemMusicPlayer parameter in iOS 9. I had some problems with the player that didn’t disconnect correctly under certain circumstances, so I switched to appplicationMusicPlayer (see Exiting the application causes an error "Message from debugger: completed due to signal 9 " )

However, as an application player, I cannot get many advantages, such as controlling the control center, displaying Bluetooth data, etc.

So, I switched to systemMusicPlayer. I also changed to Xcode 9.2 and compile the target of iOS 10.3.

Now, when I launch my application, it may take a few seconds to respond to controls such as play / pause or next / previous. My entire user interface is painfully unresponsive.

I tried to return to applicationMusicPlayer, recompile and, of course, the user interface is at normal speed.

So now I'm in a shitty position - with systemMusicPlayer, the application is barely usable, but with applicationMusicPlayer I am losing a ton of features.

This seems to be directly related either to iOS 11.2.2 on my iPhone, or to targeting on iOS 10.3 +

Does anyone have any information on what is happening and how to fix it?

EDIT: I created a very simple player and it seems to work fine in any mode, so now I'm puzzled - I will test other MP commands to see that the problem is that even when my user interface is slowing down, I'm not sure.

EDIT 2: I believe I found that the culprit is NotificationCenter, as well as getting states from MPMusicPlayerController. I updated my sample code that shows the problem. After starting, pressing the “Next” button will be slow, but pressing the “previous” may cause delays for two seconds!

Here is the basic code if you want to create a simple player. Be sure to add the three buttons to the storyboard and connect them accordingly.

// // ViewController.swift // junkplayer // import UIKit import MediaPlayer let notificationCenter = NotificationCenter.default let myMP:MPMusicPlayerController = MPMusicPlayerController.systemMusicPlayer //let myMP:MPMusicPlayerController = MPMusicPlayerController.applicationMusicPlayer() class ViewController: UIViewController { @IBOutlet weak var xxx: UIButton! @IBOutlet weak var nextbut: UIButton! @IBOutlet weak var prevbut: UIButton! var qrySongs = MPMediaQuery() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. myMP.repeatMode = MPMusicRepeatMode.none myMP.shuffleMode = MPMusicShuffleMode.off myMP.prepareToPlay() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) stopMPNotifications() startMPNotifications() } @IBAction func nextbut(_ sender: Any) { myMP.skipToNextItem() } @IBAction func prevbut(_ sender: Any) { myMP.skipToPreviousItem() } @IBAction func playbut(_ sender: UIButton) { qrySongs = MPMediaQuery.songs() myMP.setQueue(with: qrySongs) myMP.play() } func startMPNotifications(){ notificationCenter.addObserver(self, selector: #selector(showNowPlaying), name: .MPMusicPlayerControllerNowPlayingItemDidChange, object: myMP) notificationCenter.addObserver(self, selector: #selector(handlePlayState), name: .MPMusicPlayerControllerPlaybackStateDidChange, object: myMP) myMP.beginGeneratingPlaybackNotifications() } func stopMPNotifications(){ notificationCenter.removeObserver(self, name: .MPMusicPlayerControllerPlaybackStateDidChange, object: myMP) notificationCenter.removeObserver(self, name: .MPMusicPlayerControllerNowPlayingItemDidChange, object: myMP) myMP.endGeneratingPlaybackNotifications() } @objc func handlePlayState(){ if myMP.playbackState == .playing { print("handlePlayState playback state = playing") }else{ print("handlePlayState playback state NOT playing") } print("handlePlayState going to shownowplaying") showNowPlaying() } @objc func showNowPlaying(){ if myMP.nowPlayingItem != nil { print("shownowplaying nowplaying not null") } } } 
+5
source share
2 answers

The application seems to be blocked as soon as you start playing, but if you swipe up to display the Control Center and then release it, the application will start working normally.

+1
source

I have the same problem. I think something is wrong with the API. This is especially slow for large queries. But you can put a predicate in a quart, which prevents cloud music from continuing it.

0
source

Source: https://habr.com/ru/post/1274778/


All Articles