【发布时间】:2020-12-28 12:37:21
【问题描述】:
我正在制作一个带有颤振的应用程序,我想在给定 uri 的情况下使用 spotify 播放一首歌曲。我已经在我的 dart 代码中使用 spotify web api 实现了身份验证,所以我已经有了一个有效的访问令牌。我需要使用 iOS sdk 来播放带有 uri 的曲目,所以我需要快速实现它。我想要做的是将访问令牌传递给 swift 代码并将 spotify 应用程序远程对象与访问令牌连接,并跳过spotify iOS sdk quick start guide 的“设置用户授权”部分下的身份验证步骤。这是我完整的 AppDelegate.swift 代码
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, SPTAppRemoteDelegate, SPTAppRemotePlayerStateDelegate {
let SpotifyClientID = client id
let SpotifyRedirectURL = redirect uri
lazy var configuration = SPTConfiguration(
clientID: SpotifyClientID,
redirectURL: SpotifyRedirectURL
)
private let spotifyMethodChannelName = "spotify"
private var spotifyAppRemote: SPTAppRemote? = nil
private var result: FlutterResult? = nil
func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote) {
print("connected")
self.result!("success")
}
func appRemote(_ appRemote: SPTAppRemote, didDisconnectWithError error: Error?) {
print("disconnected")
}
func appRemote(_ appRemote: SPTAppRemote, didFailConnectionAttemptWithError error: Error?) {
print("failed " + error.debugDescription)
}
func playerStateDidChange(_ playerState: SPTAppRemotePlayerState) {
print("player state changed")
}
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// FirebaseApp.configure()
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let spotifyChannel = FlutterMethodChannel(name: spotifyMethodChannelName,
binaryMessenger: controller.binaryMessenger)
spotifyChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
// Note: this method is invoked on the UI thread.
self.result = result
switch(call.method){
case "connect":
let args: Dictionary<String, AnyObject> = (call.arguments as? Dictionary<String, AnyObject>)!
self.connect(accessToken: args["accessToken"] as! String)
case "playTrack":
let args: Dictionary<String, AnyObject> = (call.arguments as? Dictionary<String, AnyObject>)!
self.playTrack(uri: args["uri"] as! String)
default:
result(FlutterError(code: "METHOD CALL DOESN'T EXIST",
message: "Method call doesn't exist",
details: nil))
}
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func connect(accessToken: String) -> Void{
print(accessToken)
spotifyAppRemote = SPTAppRemote(configuration: self.configuration, logLevel: .debug)
spotifyAppRemote!.connectionParameters.accessToken = accessToken
spotifyAppRemote!.delegate = self
spotifyAppRemote!.connect()
}
private func playTrack(uri: String) -> Void{
self.spotifyAppRemote?.playerAPI?.play(uri, asRadio: false, callback: {peram1,peram2 in})
self.result!("success")
}
}
虽然相关部分是这个连接函数
private func connect(accessToken: String) -> Void{
print(accessToken)
spotifyAppRemote = SPTAppRemote(configuration: self.configuration, logLevel: .debug)
spotifyAppRemote!.connectionParameters.accessToken = accessToken
spotifyAppRemote!.delegate = self
spotifyAppRemote!.connect()
}
如果 Spotify 应用当前正在后台播放音乐,此代码将成功连接到 Spotify 应用,否则将无法连接(即使 Spotify 在后台打开但不播放音乐)。快速入门指南使用一种名为 authorizeAndPlayUri 的方法来播放曲目,但我不想授权,因为我已经有一个有效的访问令牌。即使没有使用访问令牌在后台打开,我如何连接到 spotify 应用程序?这也是我第一次使用 iOS 原生代码,所以我绝对有可能犯了一个我没有看到的与 swift 相关的错误。
【问题讨论】: