简体   繁体   English

Spotify API to Swift

[英]Spotify API to Swift

May be a simple question, but I am trying to use Spotify's API which is documented in Objective C but I have been working on it in Swift as far as I can. 可能是一个简单的问题,但我正在尝试使用Spotify的API,这是在Objective C中记录的,但我一直在Swift中一直在研究它。

@property (nonatomic, readonly, copy) NSArray *genres is one of their API protocols for the SPTartist class, however; @property(非原子,只读,复制)NSArray * genres是SPTartist类的API协议之一; I can't figure out how to use it in a working manner in swift. 我无法弄清楚如何在swift中以一种有效的方式使用它。

Here is example code of something I have gotten to work. 这是我已经开始工作的示例代码。

    func updateTrackLength(){
    if player?.currentTrackMetadata == nil {
        trackLength.text = ""
        return
    }
    let length = player?.currentTrackMetadata[SPTAudioStreamingMetadataTrackDuration] as Int
    let convert = length / 60
    let remainder = length % 60
    var newlength = String(convert)
    var newRemainder = String(remainder)


    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.trackLength.text = "Track length: " + newlength + " Minutes " + newRemainder + " Seconds"
    })
}

This uses the currentTrackMetaData and whatever SPT protocol to track the string output. 这使用currentTrackMetaData和任何SPT协议来跟踪字符串输出。 When I try using SPTArtistGenres or SPTArtistGenre in place of SPTAudioStreamingMetaDataTrackDuration, it does not work. 当我尝试使用SPTArtistGenres或SPTArtistGenre代替SPTAudioStreamingMetaDataTrackDuration时,它不起作用。 To specify my question, I want to use the currentTrackMetaData to call the genres property up above. 要指定我的问题,我想使用currentTrackMetaData来调用上面的genres属性。 Any advice is greatly appreciated! 任何意见是极大的赞赏!

This has absolutely nothing to do with Swift - if you tried to do what you're doing in Objective-C it'd fail as well. 这与Swift完全无关 - 如果你试图在Objective-C中做你正在做的事情,它也会失败。

The player's currentTrackMetadata dictionary returns bare URLs - if you just println() the response to player.currentTrackMetadata , you'll see that it contains nothing but URLs. 播放器的currentTrackMetadata字典返回裸URL - 如果您只是println()player.currentTrackMetadata的响应,您将看到它只包含URL。

The genres property is on the SPTArtist class. genres属性在SPTArtist类上。 To convert your bare URL into an SPTArtist , use the SPTRequest class (specifically, the requestItemAtURI method). 要将您的裸URL转换为SPTArtist ,请使用SPTRequest类(特别是requestItemAtURI方法)。 Once you have an SPTArtist object, you'll be able to get all the metadata you need from it, including genres. 拥有SPTArtist对象后,您将能够从中获取所需的所有元数据,包括流派。

Please see the documentation shipped with the library for details. 有关详细信息,请参阅库附带的文档。 In addition, the Simple Player demo app included with the SDK does a similar thing. 此外,SDK附带的Simple Player演示应用程序也做了类似的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM