Android Question ExoPlayer.setAudioAttributes

Pendrush

Well-Known Member
Licensed User
Longtime User
This is how I Initialize ExoPlayer:
B4X:
Dim jopl As JavaObject
jopl.InitializeNewInstance(Application.PackageName & ".srvplayer$MySimpleExoPlayerWrapper", Null)
Player = jopl
Player.Initialize("Player")
#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, "UserAgent/1.0 (Linux;Android 10.0)");
   }
}
#End If
I also need to add AudioFocus handling by ExoPlayer.
This is my Kotlin code from some other project:
Java:
val audioAttributes = AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).setContentType(C.CONTENT_TYPE_MUSIC).build()
player.setAudioAttributes(audioAttributes, true)
in this line player.setAudioAttributes(audioAttributes, true), TRUE instruct ExoPlayer to handle audio focus.

How can I translate this two lines in B4a?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim AttributesStatic As JavaObject
AttributesStatic.InitializeStatic("android.media.AudioAttributes")
Dim Builder As JavaObject
Builder.InitializeNewInstance("com.google.android.exoplayer2.audio.AudioAttributes.Builder", Null)
Builder.RunMethod("setUsage", Array(AttributesStatic.GetField("USAGE_MEDIA")))
Builder.RunMethod("setContentType", Array(AttributesStatic.GetField("CONTENT_TYPE_MUSIC")))
Dim jp As JavaObject = player1
jp.GetFieldJO("player").RunMethod("setAudioAttributes", Array(Builder.RunMethod("build", Null), True))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…