How can I listen for MediaPlayer notifications in Pythonista? I've tried this, but Pythonista crashes when the media changes.
import ui
import time
from objc_util import *
NSBundle.bundleWithPath_('/System/Library/Frameworks/MediaPlayer.framework').load()
MPMusicPlayerController = ObjCClass('MPMusicPlayerController')
NSNotificationCenter = ObjCClass('NSNotificationCenter')
def gotChange():
print('here')
@ui.in_background
def next():
time.sleep(2)
sysPlayer.play()
sysPlayer.skipToNextItem()
sysPlayer = MPMusicPlayerController.systemMusicPlayer()
sysPlayer.beginGeneratingPlaybackNotifications()
center = NSNotificationCenter.defaultCenter()
block = ObjCBlock(gotChange)
center.addObserverForName_object_queue_usingBlock_('MPMusicPlayerControllerNowPlayingItemDidChangeNotification', None, None, block)
next()