简体   繁体   中英

how to access exoplayer methods inside recycler view adapter

I have a exoplayer which plays videos. And i want to capture it current position.

So, in MainActivity

lateinit var player: SimpleExoPlayer

fun timeCapture(view: View) {
        Log.d(TAG,"${player.currentPosition}")
    }

is working fine

But, The fact is need to capture this event on Recycler view adapter

in MainActivity i'm passing data to recycler view like this,

 var viewAdapter = MainActivityAdapter(data)

Since, player is lateinit i cannot access it in adapter like below

        Log.d("logger","${MainActivity().player.currentPosition}")

this gives error.

So, any suggestions for this

You just need to create a global variable that's all. And be careful to handle errors while using that.

GlobalVariables.kt

package com.company.notexist.model

import com.google.android.exoplayer2.SimpleExoPlayer

object GlobalVariables {
    lateinit var player: SimpleExoPlayer
}

to access above player variable import it to any file

import com.company.notexist.model.GlobalVariables.player

then rest of the code is normal
...
player = ExoPlayerFactory.newSimpleInstance(this)
id_player_view.player = player
...
...

then in on adapter use it like this,

Log.d("logger","${player.currentPosition}")

but don't forget import statement and error handling(ie check for null and other things)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM