Android Question How to catch Exoplayer buttons to be able to pause a player when another starts

Sifu

Active Member
Hello,

how do I catch the clickitems of the Exoplayer when it's placed on the clv?
It seems the player buttons are not catched.
What I want is that other players stop when a player is started, now they just play thru each other.

I also tried the expandable_clv in another project and with that you can use the status of the expanded item to stop another player,
except when 2 or 3 are expanded then multiple players can play at once which I don't want.

For this project the players are put under each other and the nr of players depend on how many music tracks there are in an album.

B4X:
Sub FillCLV
    Log("start FillCLV")
    clv1.Clear
    TrkImage.Initialize("")
    
    Dim sources1 As List
    sources1.Initialize
    
    ''''''playerJ.Prepare(playerJ.CreateListSource(sources1))
        
    Dim trackpic As String
    If albumtrackpics.IsInitialized Then                  
    Log("albumtrackpics= " & albumtrackpics.Get(0))
    For i = 0 To albumtrackpics.Size -1
        trackpic = albumtrackpics.Get(i)
        Log("trackpic= " & trackpic)
        '''''playerJ.Initialize("playerJ")
        playerJ.Prepare(playerJ.CreateListSource(sources1))
        Dim p1 As B4XView = CreateItem((0xFF000000), TrkImage, playerJ)
        clv1.Add(p1,"")
    Dim tr As Int
    tr = i + 1              'there is no 0.mp3 so add 1
        DownloadImage(trackpic, TrkImageView)             'correct name is in albumtrackpics.Get(i)
        sources1.Add(playerJ.CreateUriSource("https://trackshere.com/albumtracks/" & albumnamempthree & "/" & tr & ".mp3"))
    Next
    End If
End Sub

B4X:
Sub CreateItem(clr As Int,  trkimg As ImageView, player As B4XView) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, clv1.AsView.Width,260dip) ', clv1.AsView.Height)
    p.LoadLayout("clvfront")

    TrkImageView.Bitmap = trkimg.Bitmap
    
    SimpleExoPlayerView1.Player = player
    SimpleExoPlayerView1.Tag = player
    'SimpleExoPlayerView1.ControllerTimeout = -1
    'SimpleExoPlayerView1.RemoveView
    'SimpleExoPlayerView1.UseController = False
    VideoPlayers.Put(SimpleExoPlayerView1, True)
    Dim jo As JavaObject = SimpleExoPlayerView1 'this stops controlbuttons from fading away on player
    jo.RunMethod("setControllerShowTimeoutMs", Array(0))
    jo.RunMethod("setControllerHideOnTouch", Array(False)) 'this line prevents remove of controls, when touch on outer left or right side
    Return p    
End Sub

B4X:
Sub clv1_ItemClick (Index As Int, Value As Object)
    Log("Clicked on clv1")
    Try
    'AdWebView.SendToBack
    Log("index: " & Index & "Value: " & Value)
    Dim MyVal As String = Value
    Dim item() As String = Regex.Split(",", MyVal)
    Log("item " & item(0)) 'this show Expanded=false or Expanded=True
    Dim JaNee As String = item(0)
    Dim TF() As String = Regex.Split("=", JaNee)
    Log("TF: " & TF(0))
    Dim components() As String
    components = Regex.Split("=", item(0)) 'returns: "abc", "def", "", "ghi"
    Log ("components: " & components(0))
    Dim tbState As Boolean
    If components(0) = "true" Then
        tbState = True
        Else
            tbState = False
    End If
    Private id As Int
    id = Index' + 1
    'If id = 0 And tbState = True Then
    If id = 0 Then
    playerJ.Pause
        
    End If
    Catch
        Log("itemclickerror: " & LastException.Message)
    
    End Try
End Sub

Trying to test some things which gave no result yet:
B4X:
Sub SimpleExoPlayerView1_Click
    Log(SimpleExoPlayerView1_ItemClick)
    Log(SimpleExoPlayerView1_Click)
End Sub

Private Sub Music_Click 
    Log(Music_Click)
    Log(MusicBtn)
End Sub

Private Sub VideoClips_Click
    Log(VideoClips_Click)
    Log(VideoClipsBtn)
    Log(VideoPlayers)
End Sub

Sub Player_ItemClick (Index As Int, Value As Object)
Log("playerIndex: " & Index)
Log("playerValue: " & Value)
End Sub

Does anyone have a idea how to solve this? I searched but could not find an answer, nor that it was asked before.

Many Thanks for any help!
 

Sifu

Active Member
Hi,
the 2nd line of what I added from example via the link above gives an error
B4X:
Dim jo As JavaObject = playerJ
    Dim event As Object = jo.CreateEventFromUI("com.google.android.exoplayer2.ExoPlayer$EventListener", "statechanged", False) '----this line gives an error
    jo.GetFieldJO("player").RunMethod("addListener", Array(event))
the line is exactly as the line here: https://www.b4x.com/android/forum/threads/exoplayer-play-pause-state.82648/post-524274
B4X:
java.lang.RuntimeException: java.lang.ClassNotFoundException: com.google$android$exoplayer2$ExoPlayer$EventListener
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1719)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:7864)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
Caused by: java.lang.ClassNotFoundException: com.google$android$exoplayer2$ExoPlayer$EventListener
    at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:289)
    at anywheresoftware.b4j.object.JavaObject.createEvent(JavaObject.java:253)
    at anywheresoftware.b4j.object.JavaObject.CreateEventFromUI(JavaObject.java:223)

I tried it in FillCLV sub and the CreateItem Sub, as I was not exactly sure where to put it.

Am I missing something, because the error says it can't find a class ? I use the 1.52 library.

Thanks for any help.
 
Upvote 0

Sifu

Active Member
This works, but I'm getting the same id for each player. What is the best way to give each player a unique id?
So I can pause the correct player.
I can't change playerJ to player1, player2 etc. at least that's what I think.

Thanks for any help.
 
Upvote 0

Sifu

Active Member
A Map not I think because the key must be a number, if I use a variable as key it says it cannot assign an empty value.
I was trying some things, but actually it's already a list is it not?
The image + player are both on the clvfront which is added to the clv for each track. If I click on the image then it returns the correct index nr., only the player not.
Also I tried to set the tag with an nr, but that returns only the total.
 
Upvote 0

Sifu

Active Member
My trying did not work, so I changed the concept to a single player with separate buttons, now the player itself is not visible, next and previous will load the next track and artwork as image.
Now have to find a solution for alternate fastforward and rewind as I don't know how to handle the Seekto via Java and of course the seekbar, or there must be a way to only show that. Will open a new thread for that.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…