问题描述
我正在制作一个带有抖动的应用程序,我想在给定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>)!
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本机代码,因此肯定有可能犯了一个与我无关的迅速相关错误。
解决方法
您可以尝试包含道具 { playURI:“”, } 在您的配置中
来源:https://github.com/cjam/react-native-spotify-remote/issues/75
,好吧,根据官方 Spotify sdk 文档,我们很不走运。如果 Spotify 应用当前未运行,您需要使用 authorizeAndPlayURI 来唤醒它。
https://spotify.github.io/ios-sdk/html/Classes/SPTAppRemote.html#//api/name/connect
编辑: 您可以使用 checkIfSpotifyAppIsActive 来检查 Spotify 应用当前是否在后台处于活动状态,决定是简单地连接它还是使用 authorizeAnPlayURI 来唤醒应用。
事先获取访问令牌的好处是,如果 Spotify 应用程序恰好正在运行,则不会有任何应用程序切换,事情会更顺畅。否则,应用切换并继续。