iOS: "An instance of SPAudioStreamingController is already in use."
iOS: "An instance of SPAudioStreamingController is already in use."
我正在使用 spotify-iOS-SDK 开发应用程序,我已成功将我的应用程序连接到 Spotify 并且正在播放音频,但问题是:当我关闭我的 PlaySongViewController ,我的应用程序将崩溃
"An instance of SPAudioStreamingController is already in use."
除非我在注销后用这段代码停止我的 spotifyPlayer
var spotifyPlayer: SPTAudioStreamingController?
@IBAction func closeView(_ sender: UIButton) {
print("close view")
self.dismiss(animated: true, completion: nil)
self.spotifyPlayer?.logout()
invalidateTimers()
}
func audioStreamingDidLogout(_ audioStreaming: SPTAudioStreamingController!) {
print("after logout")
try! self.spotifyPlayer?.stop()
}
如果我在此代码正常工作之前直接关闭 ViewController,问题仍然存在
self.spotifyPlayer = SPTAudioStreamingController.sharedInstance()
self.spotifyPlayer!.playbackDelegate = self
self.spotifyPlayer!.delegate = self
try! spotifyPlayer?.start(withClientId: auth.clientID)
self.spotifyPlayer!.login(withAccessToken: authSession.accessToken)
当我选择另一首歌曲再次打开我的 PlaySongViewController 时,它会崩溃
"An instance of SPAudioStreamingController is already in use."
另一个问题是,当我尝试使用非高级帐户登录时,当我打开 PlaySongViewController 时,它会显示 "Spotify Premium Required",而当我关闭我的 PlaySongViewController 和打开另一个 PlaySongViewController 播放另一首歌曲,它会再次崩溃并出现 'already in use' 错误
如果我启动了 spotifyPlayer,我可以绕过这段代码吗?
try! spotifyPlayer?.start(withClientId: auth.clientID)
或者有什么解决办法吗?
好吧,我在这里看到了两件事:
try! spotifyPlayer?.start(withClientId: auth.clientID)
那应该是 do
和 catch
,如果您查看开始签名的完整签名,它会指出它会抛出错误。相反,请确保尽可能捕获错误。
do {
try self.spotifyPlayer?.start(withClientId: auth.clientID)
} catch {
print("Failed to start with clientId")
}
重要的是不要强制 try
而是 handle errors。
You use a do-catch statement to handle errors by running a block of
code. If an error is thrown by the code in the do clause, it is
matched against the catch clauses to determine which one of them can
handle the error.
此外 SPTAudioStreamingController.sharedInstance()
上有一个 属性 值得在 do
和 catch
之前检查 player?.loggedIn
您可以使用此方法来在尝试调用 login
之前进行检查,我已经在下面的单例 login
方法中举了一个例子。
/** Returns `YES` if the receiver is logged into the Spotify service, otherwise `NO`. */
open var loggedIn: Bool { get }
其次,您最好创建一个 singleton 来处理与视图控制器交互的所有播放音乐的逻辑,这样您就不会遇到多个视图控制器试图使用相同的 spotifyPlayer
并在不需要时调用 start
。
class MusicPlayer: {
static let shared = MusicPlayer()
fileprivate let player = SPTAudioStreamingController.sharedInstance()
override init() {
player?.playbackDelegate = self
player?.delegate = self
}
func login() {
if player?.loggedIn { return }
do {
try self.spotifyPlayer?.start(withClientId: auth.clientID)
} catch {
print("Failed to start with clientId")
}
}
}
然后在视图控制器中link到MusicPlayer.shared
。
我正在使用 spotify-iOS-SDK 开发应用程序,我已成功将我的应用程序连接到 Spotify 并且正在播放音频,但问题是:当我关闭我的 PlaySongViewController ,我的应用程序将崩溃
"An instance of SPAudioStreamingController is already in use."
除非我在注销后用这段代码停止我的 spotifyPlayer
var spotifyPlayer: SPTAudioStreamingController?
@IBAction func closeView(_ sender: UIButton) {
print("close view")
self.dismiss(animated: true, completion: nil)
self.spotifyPlayer?.logout()
invalidateTimers()
}
func audioStreamingDidLogout(_ audioStreaming: SPTAudioStreamingController!) {
print("after logout")
try! self.spotifyPlayer?.stop()
}
如果我在此代码正常工作之前直接关闭 ViewController,问题仍然存在
self.spotifyPlayer = SPTAudioStreamingController.sharedInstance()
self.spotifyPlayer!.playbackDelegate = self
self.spotifyPlayer!.delegate = self
try! spotifyPlayer?.start(withClientId: auth.clientID)
self.spotifyPlayer!.login(withAccessToken: authSession.accessToken)
当我选择另一首歌曲再次打开我的 PlaySongViewController 时,它会崩溃
"An instance of SPAudioStreamingController is already in use."
另一个问题是,当我尝试使用非高级帐户登录时,当我打开 PlaySongViewController 时,它会显示 "Spotify Premium Required",而当我关闭我的 PlaySongViewController 和打开另一个 PlaySongViewController 播放另一首歌曲,它会再次崩溃并出现 'already in use' 错误
如果我启动了 spotifyPlayer,我可以绕过这段代码吗?
try! spotifyPlayer?.start(withClientId: auth.clientID)
或者有什么解决办法吗?
好吧,我在这里看到了两件事:
try! spotifyPlayer?.start(withClientId: auth.clientID)
那应该是 do
和 catch
,如果您查看开始签名的完整签名,它会指出它会抛出错误。相反,请确保尽可能捕获错误。
do {
try self.spotifyPlayer?.start(withClientId: auth.clientID)
} catch {
print("Failed to start with clientId")
}
重要的是不要强制 try
而是 handle errors。
You use a do-catch statement to handle errors by running a block of code. If an error is thrown by the code in the do clause, it is matched against the catch clauses to determine which one of them can handle the error.
此外 SPTAudioStreamingController.sharedInstance()
上有一个 属性 值得在 do
和 catch
之前检查 player?.loggedIn
您可以使用此方法来在尝试调用 login
之前进行检查,我已经在下面的单例 login
方法中举了一个例子。
/** Returns `YES` if the receiver is logged into the Spotify service, otherwise `NO`. */
open var loggedIn: Bool { get }
其次,您最好创建一个 singleton 来处理与视图控制器交互的所有播放音乐的逻辑,这样您就不会遇到多个视图控制器试图使用相同的 spotifyPlayer
并在不需要时调用 start
。
class MusicPlayer: {
static let shared = MusicPlayer()
fileprivate let player = SPTAudioStreamingController.sharedInstance()
override init() {
player?.playbackDelegate = self
player?.delegate = self
}
func login() {
if player?.loggedIn { return }
do {
try self.spotifyPlayer?.start(withClientId: auth.clientID)
} catch {
print("Failed to start with clientId")
}
}
}
然后在视图控制器中link到MusicPlayer.shared
。