【发布时间】:2016-09-14 02:44:09
【问题描述】:
我正在 OS X 上用 Swift 编写应用程序。有没有办法在 iTunes 中获取当前正在播放的音频?我想获取标题、艺术家等详细信息。
我已经找到了什么:
【问题讨论】:
我正在 OS X 上用 Swift 编写应用程序。有没有办法在 iTunes 中获取当前正在播放的音频?我想获取标题、艺术家等详细信息。
我已经找到了什么:
【问题讨论】:
下面是一个简单的例子,如何从当前音轨中获取属性:
在(El Capitan v10.11.5、iTunes 12.4 和 Xcode 7.3)上测试。
import Cocoa
import ScriptingBridge
@objc protocol iTunesApplication {
optional func currentTrack()-> AnyObject
optional var properties: NSDictionary {get}
//if you need another object or method from the iTunes.h, you must add it here
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
let iTunesApp: AnyObject = SBApplication(bundleIdentifier: "com.apple.iTunes")!
let trackDict = iTunesApp.currentTrack!().properties as Dictionary
if (trackDict["name"] != nil) {// if nil then no current track
print(trackDict["name"]!) // print the title
print(trackDict["artist"]!)
print(trackDict["album"]!)
print(trackDict["playedCount"]!)
// print(trackDict) // print the dictionary
}
}
}
【讨论】:
Value of type 'AnyObject' has no member 'currentTrack' 在 trackDict 声明行
com.apple.iTunes 更改为com.apple.Music。