gpt4 book ai didi

ios - Spotify SDK 播放器不工作 : Error Domain=com. spotify.ios-sdk.playback Code=1 "The operation failed due to an unspecified issue."

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:32 24 4
gpt4 key购买 nike

我目前正在开发一个 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 "The operation failed due to an unspecified issue." UserInfo={NSLocalizedDescription=The operation failed due to an unspecified issue.}

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

// 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] Caching allowed 1

来自以下部分:

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

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

最佳答案

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

    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")
}

关于ios - Spotify SDK 播放器不工作 : Error Domain=com. spotify.ios-sdk.playback Code=1 "The operation failed due to an unspecified issue.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47376096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com