在Pythonis中收听MediaPlayer更改通知

2024-09-28 03:15:02 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Pythonista中侦听MediaPlayer通知?我试过这个,但当媒体改变时,Python会崩溃。你知道吗

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()

Tags: importnoneuitimedefblocknextpythonista

热门问题