在 OS X 上从 iTunes 中当前播放的音频中获取音频元数据
Get audio metadata from current playing audio in iTunes on OS X
我正在 Swift 在 OS X 上编写一个应用程序。有没有办法在 iTunes 中获取当前正在播放的音频?我想获取标题、艺术家等详细信息
我已经发现:
Here is an Objective C example, but not all those methods are available with Swift.
This example uses AppleScript, which I might have to do. But I'd rather use Swift if possible.
这是一个如何从当前轨道获取属性的简单示例:
已在 (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
}
}
}
我正在 Swift 在 OS X 上编写一个应用程序。有没有办法在 iTunes 中获取当前正在播放的音频?我想获取标题、艺术家等详细信息
我已经发现:
Here is an Objective C example, but not all those methods are available with Swift.
This example uses AppleScript, which I might have to do. But I'd rather use Swift if possible.
这是一个如何从当前轨道获取属性的简单示例:
已在 (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
}
}
}