Spotify SDK 播放器不工作:错误 Domain=com.spotify.ios-sdk.playback Code=1 “由于未指定的问题,操作失败。”

Posted

技术标签:

【中文标题】Spotify SDK 播放器不工作:错误 Domain=com.spotify.ios-sdk.playback Code=1 “由于未指定的问题,操作失败。”【英文标题】:Spotify SDK player not working: Error Domain=com.spotify.ios-sdk.playback Code=1 "The operation failed due to an unspecified issue." 【发布时间】:2018-05-02 17:34:57 【问题描述】:

我目前正在开发一个 ios 应用程序来登录我的 Spotify 帐户并在其中播放歌曲。

这是我的代码:

import UIKit
import AVKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, 
SPTAudioStreamingDelegate 

var window: UIWindow?

let kClientId = "hidden------my client ID"
let kRedirectUrl = URL(string: "spotify-study2-login://return-after-login")

var session: SPTSession?
var player: SPTAudioStreamingController?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool 

    // set up Spotofy
    SPTAuth.defaultInstance().clientID = kClientId
    SPTAuth.defaultInstance().redirectURL = kRedirectUrl
    SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope] as [AnyObject]
    let loginUrl = SPTAuth.defaultInstance().spotifyAppAuthenticationURL()
    application.open(loginUrl!)

    return true


// handle auth
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool 

    if SPTAuth.defaultInstance().canHandle(url) 
        SPTAuth.defaultInstance().handleAuthCallback(withTriggeredAuthURL: url, callback:  error, session in
            if error != nil 
                print("*** Auth error: \(String(describing: error))")
            
            // Call the -loginUsingSession: method to login SDK
            self.loginUsingSession(session: session!)
        )
        return true
    

    return false


func loginUsingSession(session: SPTSession) 
   // Get the player Instance
    player = SPTAudioStreamingController.sharedInstance()
    if let player = player 
        player.delegate = self
        // start the player (will start a thread)
        try! player.start(withClientId: kClientId)
        // Login SDK before we can start playback
        player.login(withAccessToken: session.accessToken)

        let urlStr = "spotify:track:3yMPqvbPNaL5DUDOmwEr6l" // a song I choose. I already confirmed this song really exsits.
        self.player?.playSpotifyURI(urlStr, startingWith: 0, startingWithPosition: 0, callback:  error in
            if error != nil 
                print("*** failed to play: \(String(describing: error))")
                return
             else 
                print("play")
            
        )

    


// MARK: SPTAudioStreamingDelegate.
func audioStreamingDidLogin(audioStreaming: SPTAudioStreamingController!) 

    let urlStr = "spotify:track:3yMPqvbPNaL5DUDOmwEr6l" // a song I choose. I already confirmed this song really exsits.
    player!.playSpotifyURI(urlStr, startingWith: 0, startingWithPosition: 0, callback:  error in
        if error != nil 
            print("*** failed to play: \(String(describing: error))")
            return
         else 
            print("play")
        
    )


func applicationWillResignActive(_ application: UIApplication) 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

    //log
    print("func applicationWillResignActive has been called")


func applicationDidEnterBackground(_ application: UIApplication) 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    //log
    print("func applicationDidEnterBackground has been called")


func applicationWillEnterForeground(_ application: UIApplication) 
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

    //log
    print("func applicationWillEnterForeground has been called")


func applicationDidBecomeActive(_ application: UIApplication) 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    //log
    print("applicationDidBecomeActive")



func applicationWillTerminate(_ application: UIApplication) 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    //log
    print("func applicationWillTerminate has been called")


当我运行调试器时,我发现一条错误消息说:

Error Domain=com.spotify.ios-sdk.playback Code=1 "操作失败 由于未指定的问题。” UserInfo=NSLocalizedDescription=The 由于未指定的问题,操作失败。

此错误消息来自以下部分:

// MARK: SPTAudioStreamingDelegate.
func audioStreamingDidLogin(audioStreaming: 
SPTAudioStreamingController!) 

    let urlStr = "spotify:track:3yMPqvbPNaL5DUDOmwEr6l" // a song I choose. I already confirmed this song really exsits.
    player!.playSpotifyURI(urlStr, startingWith: 0, startingWithPosition: 0, callback:  error in
        if error != nil 
            print("*** failed to play: \(String(describing: error))")
            return
         else 
            print("play")
        
    )

另外,我也找到了

2017-11-19 19:31:04.050872+0900 SpotifyStudy2[756:110616] 缓存 允许 1

从下面的部分:

// Login SDK before we can start playback
        player.login(withAccessToken: session.accessToken)

谷歌搜索这个问题花费了我很多时间,但我仍然没有找到一个很好的答案来解决这个问题。 如果您知道此错误的具体含义,您的回答将对我有很大帮助......!提前谢谢你。

【问题讨论】:

@Chika 有什么解决办法吗? 【参考方案1】:

我遇到了同样的问题,我已经使用 belove 代码解决了

    do 
        try SPTAudioStreamingController.sharedInstance()?.start(withClientId: SPTAuth.defaultInstance().clientID, audioController: nil, allowCaching: true)
            SPTAudioStreamingController.sharedInstance().delegate = self
            SPTAudioStreamingController.sharedInstance().playbackDelegate = self
            SPTAudioStreamingController.sharedInstance().diskCache = SPTDiskCache() /* capacity: 1024 * 1024 * 64 */
            SPTAudioStreamingController.sharedInstance().login(withAccessToken: "Token here")
     catch _ 
        print("catch")
    

【讨论】:

以上是关于Spotify SDK 播放器不工作:错误 Domain=com.spotify.ios-sdk.playback Code=1 “由于未指定的问题,操作失败。”的主要内容,如果未能解决你的问题,请参考以下文章

Spotify iOS SDK - 无后台播放

播放单轨Spotify SDK

如何将从 Spotify 流式传输的音乐可播放数据推送到不使用 Spotify 提供的 SDK 的设备

使用android sdk登录spotify时如何获取帐户类型

Spotify SDK iOS SDK 403 错误

Spotify 网络播放 SDK 集成