Android Question ExoPlayer User-agent

Pendrush

Well-Known Member
Licensed User
Longtime User
Is there any chance to change ExoPlayer user-agent?
Default user agent is: AppName/AppVerison (Linux;Android 7.0) ExoPlayerLib/2.5.3

As app get Cyrillic name, User-Agent become Cyrillic also and that create a lot of problems on ShoutCast servers.
 

Pendrush

Well-Known Member
Licensed User
Longtime User
I'm using: Plejer.Prepare(Plejer.CreateHLSSource(streamUrl) and Plejer.Prepare(Plejer.CreateUriSource(streamUrl)).
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Different approach here.

1. Add this code:
B4X:
#if Java
import anywheresoftware.b4a.keywords.B4AApplication;
import android.content.pm.PackageManager.NameNotFoundException;
import anywheresoftware.b4a.objects.SimpleExoPlayerWrapper;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
public static class MySimpleExoPlayerWrapper extends SimpleExoPlayerWrapper {
@Override
public DefaultDataSourceFactory createDefaultDataFactory() {
     return new DefaultDataSourceFactory(BA.applicationContext, "custom user agent here");
   }
}

#End If

2. Use the new class:
B4X:
Sub Globals
   Private SimpleExoPlayerView1 As SimpleExoPlayerView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim jo As JavaObject
   jo.InitializeNewInstance(Application.PackageName & ".main$MySimpleExoPlayerWrapper", Null)
   SimpleExoPlayerView1 = jo
   '...
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
ExoPlayer is in service "srvPlayer" I have changed intialize line, but after try to playstream i get this error
Error occurred on line: 184 (srvPlayer)
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.exoplayer2.SimpleExoPlayer.prepare(com.google.android.exoplayer2.source.MediaSource)' on a null object reference
at anywheresoftware.b4a.objects.SimpleExoPlayerWrapper.Prepare(SimpleExoPlayerWrapper.java:198)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Line 184: Plejer.Prepare(Plejer.CreateUriSource(StreamUrlStanice))

Initialize line on service_create
B4X:
Dim jo As JavaObject
jo.InitializeNewInstance(Application.PackageName & ".srvplayer$MySimpleExoPlayerWrapper", Null)
Plejer = jo
 
Upvote 0

MAGAREY

Member
Licensed User
Longtime User
Different approach here.

1. Add this code:
B4X:
#if Java
import anywheresoftware.b4a.keywords.B4AApplication;
import android.content.pm.PackageManager.NameNotFoundException;
import anywheresoftware.b4a.objects.SimpleExoPlayerWrapper;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
public static class MySimpleExoPlayerWrapper extends SimpleExoPlayerWrapper {
@Override
public DefaultDataSourceFactory createDefaultDataFactory() {
     return new DefaultDataSourceFactory(BA.applicationContext, "custom user agent here");
   }
}

#End If

2. Use the new class:
B4X:
Sub Globals
   Private SimpleExoPlayerView1 As SimpleExoPlayerView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim jo As JavaObject
   jo.InitializeNewInstance(Application.PackageName & ".main$MySimpleExoPlayerWrapper", Null)
   SimpleExoPlayerView1 = jo
   '...
the global variable is simpleexoplayer
 
Upvote 0
Top