【发布时间】:2011-07-08 22:54:19
【问题描述】:
我正在编写一个点唱机应用程序,它使用 php/js 作为前端并使用 iTunes 作为后端。问题是我需要一种方法来判断歌曲何时停止在 iTunes 中播放。我曾想过使用空闲脚本通过 applescript 轮询 iTunes。但是,我必须每隔这么多秒进行一次轮询,而不是在歌曲停止播放时运行一个 AppleScript 的事件。有什么想法吗?
【问题讨论】:
标签: applescript itunes polling
我正在编写一个点唱机应用程序,它使用 php/js 作为前端并使用 iTunes 作为后端。问题是我需要一种方法来判断歌曲何时停止在 iTunes 中播放。我曾想过使用空闲脚本通过 applescript 轮询 iTunes。但是,我必须每隔这么多秒进行一次轮询,而不是在歌曲停止播放时运行一个 AppleScript 的事件。有什么想法吗?
【问题讨论】:
标签: applescript itunes polling
只要状态发生变化,iTunes 就会发出一个系统范围的通知,称为“com.apple.itunes.playerInfo”。因此,如果您可以从 php 注册系统通知 (NSDistributedNotificationCenter),那么这将是一种方法,而不是轮询。快速搜索一下如何从 python 中执行此操作...here。
【讨论】:
我不完全确定是否存在允许您执行此操作的方法,但现在您始终可以使用 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
如果您有任何问题,请尽管提问。 :)
【讨论】: