Jonas Frei
Jonas Frei

Reputation: 382

Flutter - Get data with an Event Channel from Kotlin to Dart

I have the following problem that I am already working on for over 20 hours: I want to use an Event Channel to get a data stream from the Spotify SDK. On the native side, I can automatically display the status of a current song by subscribing to my PlayerState. My goal is to be able to access this data stream with my Flutter app. On the native side I can output the data flow without problems. But I also want to be able to access this data in my Flutter App. The problem is that I do not get the data from Kotlin to Dart. I can not execute the command mEventSink?.success(position) because the mEventSink is zero. It would be really great if someone could help me with this problem.

//...

class Spotifysdk04Plugin(private var registrar: Registrar): MethodCallHandler, EventChannel.StreamHandler {
    //...
    private var mEventSink: EventChannel.EventSink? = null

  companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
      val channel = MethodChannel(registrar.messenger(), "spotifysdk")
      channel.setMethodCallHandler(Spotifysdk04Plugin(registrar))

        val eventChannel = EventChannel(registrar.messenger(), "timerStream")
        eventChannel.setStreamHandler(Spotifysdk04Plugin(registrar))
    }
  }

  override fun onMethodCall(call: MethodCall, result: Result) {
    if (call.method == "loginAppRemote") {
        //...
    } else if(call.method == "initEventStream") {
        try {
            spotifyAppRemote!!.playerApi.subscribeToPlayerState()
                    .setEventCallback { playerState: PlayerState? ->
                        Log.d("test", "test24")
                        var position = playerState!!.playbackPosition.toDouble()
                        Log.d("playbackPosition1", position.toString())
                        if(mEventSink != null) {
                            Log.d("test", "test25")
                            mEventSink?.success(position)
                        } else {
                            Log.d("test", "mEventSink == null")
                        }
                    }
        } catch (err:Throwable) {
            Log.v("initEventStreamError",err.message.toString())
            result.success(false)
        }
    } else {
      result.notImplemented()
    }
  }

    override fun onCancel(arguments: Any?) {
        mEventSink = null
    }

    override fun onListen(arguments: Any?, eventSink: EventChannel.EventSink) {
        mEventSink = eventSink
    }
}

Upvotes: 0

Views: 2407

Answers (1)

Jonas Frei
Jonas Frei

Reputation: 382

I found a solution:

override fun onListen(p0: Any?, p1: EventChannel.EventSink?) {
        mEventSink = p1
        Log.d("test", "test1")
        if(spotifyAppRemote == null) {
            Log.d("test", "test2")
        }

        val connectionParams = ConnectionParams.Builder(clientId)
                .setRedirectUri(redirectUri)
                .showAuthView(true)
                .build()

        SpotifyAppRemote.connect(registrar.context(), connectionParams, object : Connector.ConnectionListener {
            override fun onConnected(appRemote: SpotifyAppRemote) {
                spotifyAppRemote = appRemote
                if(spotifyAppRemote != null) {
                    Log.d("test", "test3")
                    spotifyAppRemote!!.playerApi.subscribeToPlayerState()
                            .setEventCallback { playerState: PlayerState? ->
                                Log.d("test", "test24")
                                var position = playerState!!.playbackPosition.toDouble()
                                Log.d("playbackPosition1", position.toString())
                                if(mEventSink != null) {
                                    Log.d("test", "test25")
                                    mEventSink?.success(position)
                                } else {
                                    Log.d("test", "mEventSink == null")
                                }
                            }
                }

                Log.d("Spotify App Remote Login", "Connected!")
            }

            override fun onFailure(throwable: Throwable) {
                Log.e("Spotify App Remote Login", "Error!", throwable)
            }
        })
    }

Upvotes: 2

Related Questions