我正在写一个自动点唱机应用程序,它使用php/js作为前端,使用itunes作为后端。问题是,我需要一种方法来判断一首歌何时在itunes中停止播放。我曾想过使用空闲的脚本通过applescript来轮询itunes。但是,我必须每隔几秒钟轮询一次,而不是想要一个事件在歌曲停止播放时运行applescript。有什么想法吗?
发布于 2011-07-09 09:06:20
我不能完全确定是否存在允许您这样做的方法,但现在您可以始终使用iTunes的player state属性,该属性通过返回以下五个值之一来告诉您iTunes当前正在执行的操作:
playing, stopped, paused, fast forwarding, rewinding使用该属性,您可以创建一个不包含任何命令的repeat until player state is stopped循环(本质上是等到当前播放的歌曲停止),然后在循环之后执行您想要的任何东西。翻译成代码,这段话是这样的:
tell application "iTunes"
repeat until player state is stopped
--do nothing until the song currently playing is stopped...
end repeat
--[1]...and then execute whatever you want here
end tell可选
如果您只想运行脚本一次,那么您可以将上面的脚本插入到一个无限的repeat循环中,尽管您可能想要先运行一点delay以允许您开始一首歌曲。否则,1将在您启动脚本后立即执行(假设没有正在使用的歌曲)。
代码
repeat
delay 60 --1 minute delay
tell application "iTunes"
repeat until player state is stopped
--wait
end repeat
...
end tell
end repeat如果你有任何问题,尽管问。:)
发布于 2011-07-09 14:24:54
每当iTunes的状态发生变化时,它都会发出一个系统范围的通知,称为"com.apple.itunes.playerInfo“。因此,如果你可以注册系统通知(NSDistributedNotificationCenter)从php,那么这将是一个方法,而不是轮询。快速搜索一下,就会发现如何在python中执行此操作...here。
https://stackoverflow.com/questions/6631376
复制相似问题