tidal-music / tidal-sdk-ios

TIDAL SDK for iOS
Apache License 2.0
16 stars 1 forks source link

Trouble intializing the player #50

Open iNathanZ opened 3 months ago

iNathanZ commented 3 months ago

Hello, I have some troubles when I want to initialize the player. I did the same as the template project / documentation asks to.

Here's my code:

import Foundation
import Auth
import UIKit
import SafariServices
import Player
import EventProducer

public typealias PlayerState = State

class TidalManager: ObservableObject, PlayerListener {

    static let shared = TidalManager()

    @Published var showAuthSheet: Bool = false
    @Published var authSheetUrl: String = ""
    @Published var credentials: Credentials?
    @Published var playerState: PlayerState = State.IDLE

    private var eventSender: TidalEventSender {
        .shared
    }

    private var player: Player?

    let authConfig = AuthConfig(clientId: Constants.Tidal.clientId, credentialsKey: "TIDAL_FAN_APP")

    func appInitAuth() {
        TidalAuth.shared.config(config: authConfig)
        initEventSender()
    }

    func initAuth() {
        let auth = TidalAuth.shared
        let loginConfig = LoginConfig()
        let redirectUri = URL(string: "https://www.focal.com/")!

        guard let loginURL = auth.initializeLogin(redirectUri: redirectUri.absoluteString, loginConfig: loginConfig) else {
            print("Something is wrong")
            return
        }
        showAuthSheet = true
        authSheetUrl = loginURL.absoluteString
    }

    func handleRedirect(url: URL) async {
        do {
            try await TidalAuth.shared.finalizeLogin(loginResponseUri: url.absoluteString)
            DispatchQueue.main.async {
                UserDefaults.standard.set(true, forKey: "TidalAuth")
                self.showAuthSheet = false
                self.authSheetUrl = ""
            }
            getCredentials()
        } catch {
            print("Failed to finalize login: \(error)")
        }
    }

    func getCredentials() {
        Task {
            do {
                let credentialsProvider: CredentialsProvider = TidalAuth.shared
                let credentials = try await credentialsProvider.getCredentials()
                if let vToken = credentials.token {
                    DispatchQueue.main.async {
                        self.credentials = credentials
                        SourceManager.shared.isTidalOK = true
                        Network.shared.initAuthenticatedSession(with: vToken, sessionType: .tidal)
                    }
                }
                UserDefaults.standard.setValue(credentials.token, forKey: "focal_tidal_token")
                initEventSender()
                initPlayer()
            } catch {
                print("Error while getting credentials: \(error)")
            }
        }
    }

    private func initEventSender() {
        let config = EventConfig(
            credentialsProvider: TidalAuth.shared,
            maxDiskUsageBytes: 1000000,
            blockedConsentCategories: []
        )

        eventSender.config(config)
    }

    private func initPlayer() {
        player = Player.bootstrap(
            listener: self,
            credentialsProvider: TidalAuth.shared,
            eventSender: eventSender
        )
    }

    func playTrack() {
        player?.load(
            MediaProduct(
                productType: ProductType.TRACK,
                productId: "240549030"  //test "3993178"
            )
        )
        player?.play()
    }

    func stateChanged(to state: State) {
        playerState = state
    }

    func ended(_ mediaProduct: MediaProduct) {
        //
    }

    func mediaTransitioned(to mediaProduct: MediaProduct, with playbackContext: PlaybackContext) {
        //
    }

    func streamingPrivilegesLost(to device: String?) {
        //
    }

    func failed(with error: PlayerError) {
        //
    }

    func djSessionStarted(_ metadata: DJSessionMetadata) {
        //
    }

    func djSessionEnded(with reason: DJSessionEndReason) {
        //
    }

    func djSessionTransitioned(to transition: DJSessionTransition) {
        //
    }
}

Everything works when i log-in with the webview, but when I try to play a song, with my playTrack function, my app crashes with that stacktrace:

Capture d’écran 2024-07-31 à 14 56 21
ceciliasaraiva commented 3 months ago

Hello @iNathanZ! Thanks for reaching out. Yes, we need to update that. For now, you can use player.setUpUserConfiguration(UserConfiguration(userId: -1, userClientId: -1)). Please let me know how it goes. Thanks!