Android Question stream using exoplayer

apti

Member
I have been looking over this and found most of my answers, however this one eludes me.
I want exoplayer to play a url stream but I am lost

an example url is... http://listen.trancebase.fm/tunein-mp3-pls all the urls are similar in that no file is given. These are the urls that work in audacious and windows media.

the code I used is below and I am sure I am wrong somehow because I do not quite understand the create things. MPL is defined as a simpleexoplayer.

the play sub:
public Sub PlayURL
    Log("mpf playurl "&RadioStationURL)
    Service.StartForeground(2,CreateNotification("MPFree playing Radio"))
    MPL.Prepare(MPL.CreateHLSSource(RadioStationURL))
    MPL.Play
End Sub

I have tried createsmoothstreamingsource also. I get no errors, just no sound either.
 
Solution
I got it. you are correct in that I needed createurisource but since it was not an https and was http I ran into those annoying google permissions. I had to add this to the manifest for the program and I am now working.
I thank you for the kick in the butt to make me look in other areas.

for the rest if you run into this...

Solution is in the manifest editor add this line
SetApplicationAttribute(android:usesCleartextTraffic,"true")

drgottjr

Expert
Licensed User
Longtime User
it's not an exo "stream".
try:
B4X:
    webview.LoadUrl("http://listen.trancebase.fm/tunein-mp3-pls")
 
Upvote 0

apti

Member
it's not an exo "stream".
try:
B4X:
    webview.LoadUrl("http://listen.trancebase.fm/tunein-mp3-pls")
Wouldn't that just bring up a web page? that is not what I want to do. I never want a web page showing up. Only audio stream. All the audio players including android based seem to pick up that stream and play fine. Maybe I don't understand the answer well enough?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
how are you loading the url with exo? CreateUriSource works.
 
Upvote 0

apti

Member
how are you loading the url with exo? CreateUriSource works.
actually that does not work... I put in the createurisource and it crashes as soon as it runs...
This is the line that the error happens on.
vvvvvvvvv
HttpDataSourceFactory.InitializeNewInstance("androidx.media3.datasource.DefaultHttpDataSource$Factory", Null)

This is the error generated by that line.
vvvvvvvvvv
java.lang.ClassNotFoundException: androidx$media3$datasource$DefaultHttpDataSource$Factory

I am not a newbie programmer but new to b4x. So I am assuming there is something I am missing. the IDE does not show any error until it runs that line. just to be safe below is the entire createurisource code. The line in question I wondered about the use of the "$" instead of a period. I tried it both ways and same crash.


createUriSource:
Private Sub CreateUriSource(Uri As String) As Object
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim HttpDataSourceFactory As JavaObject
    HttpDataSourceFactory.InitializeNewInstance("androidx.media3.datasource.DefaultHttpDataSource$Factory", Null)
    HttpDataSourceFactory.RunMethod("setAllowCrossProtocolRedirects", Array(True))
    Dim DataSource As JavaObject = HttpDataSourceFactory.RunMethod("createDataSource", Null)
    Dim DataSourceFactory As JavaObject = ctxt.CreateEvent("androidx.media3.datasource.DataSource.Factory", "", DataSource)
    Dim ProgressiveMediaSourceFactory As JavaObject
    ProgressiveMediaSourceFactory.InitializeNewInstance("androidx.media3.exoplayer.source.ProgressiveMediaSource.Factory", Array(DataSourceFactory))
    Dim MediaItem As JavaObject
    MediaItem.InitializeStatic("androidx.media3.common.MediaItem")
    Return ProgressiveMediaSourceFactory.RunMethod("createMediaSource", Array(MediaItem.RunMethod("fromUri", Array(Uri))))
    
End Sub
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
actually that does not work
actually, that does work. i don't know what all that code you posted is. i hear the music... sorry.
 
Upvote 0

apti

Member
actually, that does work. i don't know what all that code you posted is. i hear the music... sorry.
the code posted was what I got for createurisource. if you don't recognize it then I must have something strange. that code is what is called from the prepare method of exoplayer example... exoplayer.prepare(createurisource(url goes here)) which goes to the code above then returns whatever was needed by exoplayer. Like I said I am new to b4x so if I am doing something wrong, correct me so I learn.
actually, that does work. i don't know what all that code you posted is. i hear the music... sorry.
ok, I think I see what you are talking about. the createurisource from the exoplayer is what I should use, when I do that I get no crashes but I also do not get anything playing. Different problem so why do you hear music and I do not? mp3 plays fine.
 
Upvote 0

apti

Member
I got it. you are correct in that I needed createurisource but since it was not an https and was http I ran into those annoying google permissions. I had to add this to the manifest for the program and I am now working.
I thank you for the kick in the butt to make me look in other areas.

for the rest if you run into this...

Solution is in the manifest editor add this line
SetApplicationAttribute(android:usesCleartextTraffic,"true")
 
Upvote 0
Solution
Top