Android Question Definitely SOLVED : Send audio to headphone or to internal speaker

coslad

Well-Known Member
Licensed User
Longtime User
Hi

i want to use this gadget with my b4a app :

pressy-1.jpg


when i insert it into the headphone jack hole , the phone recognizes it as a headphone and the audio is directed to it , but , if i before install the "pressy" app , when i put the jack inside the hole, the system asks me if i want to use the phone as normal or if i want to send the audio to the headphone , and if i reply yes , the audio is sent to the internal speaker and mic , even thought the jack is inserted into the hole .

So , knows somebody the code to do that ?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
I tried the code :

B4X:
Sub SetEarPhone(Value As Boolean)
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    Dim mode As Int
    If Value Then mode = 2 Else mode = 0
    r.RunMethod2("setMode", mode, "java.lang.int")
End Sub

but even with false or true , the audio coming from a call remain into the headphone and does0t go to the phone internal ear speaker .

You can try with a simple external headphone (earphone)
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
This is the solution :

B4X:
Dim jo As JavaObject
    jo.InitializeContext
  
    Dim r As Reflector
    r.Target = jo.RunMethod("getSystemService", Array("audio"))
    r.RunMethod4("setWiredDeviceConnectionState", Array (4, 0, "device"), _
           Array As String("java.lang.int", "java.lang.int", "java.lang.String"))

last question :

how can trap the jack plugged in (intent) ?
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
Need help again

the solution works fine until android 7, but on android 7 it cames out this error :

B4X:
java.lang.NoSuchMethodException: setWiredDeviceConnectionState [int, int, class java.lang.String]


manifest:

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="21"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
any idea?
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
this is the error :

B4X:
java.lang.NoSuchMethodException: setWiredDeviceConnectionState [int, int, class java.lang.String]
    at java.lang.Class.getMethod(Class.java:1981)
    at java.lang.Class.getDeclaredMethod(Class.java:1960)
    at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod4(Reflection.java:857)
    at b4a.smart.home.main._button4_click(main.java:547)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:6205)
    at android.widget.TextView.performClick(TextView.java:11103)
    at android.view.View$PerformClick.run(View.java:23653)
    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:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

even the Jmu5667 example code has the same problem with android 7 :

https://www.b4x.com/android/forum/threads/mediasessions.62525/page-3

you can test , with this code the app crash :
B4X:
Dim jo AsJavaObject
 jo.InitializeContext
 Dim r AsReflector
 r.Target = jo.RunMethod("getSystemService", Array("audio"))
 r.RunMethod4("setWiredDeviceConnectionState", Array (4, 0, "device"), _ArrayAsString("java.lang.int", "java.lang.int", "java.lang.String"))
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
What does it mean? My app can run on Android 6 but not on Android 7?
 
Upvote 0

coslad

Well-Known Member
Licensed User
Longtime User
That means that there seem to be no such method inside the android api.
https://developer.android.com/refer...oManager.html?q=setWiredDeviceConnectionState

DonManfred maybe you wrong becouse the method exist but it has 4 arguments and not 3 .

Infact if i write :

r.RunMethod4("setWiredDeviceConnectionState", Array (4, 0, "device"), _ArrayAsString("java.lang.int", "java.lang.int", "java.lang.String", "java.lang.String"))

the error is :

java.lang.IllegalArgumentException: Wrong number of arguments; expected 4, got 3

but i don't know what to write as the fourth parameter ,if i write this:

r.RunMethod4("setWiredDeviceConnectionState", Array (4, 0, "address", "device"), Array As String("java.lang.int", "java.lang.int", "java.lang.String", "java.lang.String"))

it doesn't crashs anymore but it doesn't works , i found this code :

B4X:
//int type, int state, String address, String name
                m = am.getClass().getMethod("setWiredDeviceConnectionState", Integer.TYPE, Integer.TYPE, String.class, String.class);
                m.setAccessible(true);
                Object[] objArr = new Object[4];
                objArr[0] = (i.getIntExtra("microphone", 0) == 0) ? 8 : 4;
                objArr[1] = i.getIntExtra("state", 0);
                objArr[2] = i.getStringExtra("address");
                objArr[3] = i.getStringExtra("name");
                m.invoke(am, objArr);
 
Last edited:
Upvote 0
Top