Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble intializing the player #50

Open
iNathanZ opened this issue Jul 31, 2024 · 1 comment
Open

Trouble intializing the player #50

iNathanZ opened this issue Jul 31, 2024 · 1 comment

Comments

@iNathanZ
Copy link

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
Copy link
Contributor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants