【问题标题】:Get audio metadata from current playing audio in iTunes on OS X从 OS X 上 iTunes 中当前播放的音频中获取音频元数据
【发布时间】:2016-09-14 02:44:09
【问题描述】:

我正在 OS X 上用 Swift 编写应用程序。有没有办法在 iTunes 中获取当前正在播放的音频?我想获取标题、艺术家等详细信息。

我已经找到了什么:

  1. Here is an Objective C example, but not all those methods are available with Swift.

  2. This example uses AppleScript, which I might have to do. But I'd rather use Swift if possible.

【问题讨论】:

    标签: swift macos audio itunes


    【解决方案1】:

    下面是一个简单的例子,如何从当前音轨中获取属性:

    在(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
            }
        }
    }
    

    【讨论】:

    • 在 Xcode 11 中失败,Value of type 'AnyObject' has no member 'currentTrack'trackDict 声明行
    • 尝试将com.apple.iTunes 更改为com.apple.Music
    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多