MediaRoute and routing audio

4.1k views Asked by At

I have a Samsung Galaxy S3 (unsecured, currently), and I have a problem sometimes, for example, if I plug in my headphones to listen to FM radio and suddenly I close the application and disconnect the headphones, the phone still believe having one plugged in, so the sounds (speakers and microphones) are routes to the headphone jack, so when I want to talk to someone, no sound.

I do not know if I explain well.

I want a method to direct the sound to the normal state (speakers and microphones) when I click a button. I research something, but I get nothing. I think that this code is the right way, but I need some help and some advice.

This is my code:

public void route(Context context){
        String outputDeviceName, outputDeviceName2;
        try {
            MediaRouter media_route = (MediaRouter)getBaseContext().getSystemService(Context.MEDIA_ROUTER_SERVICE);
            Class mediaRouterClass = Class.forName("android.media.MediaRouter");
            Method getSystemAudioRouteMethod = mediaRouterClass.getMethod("getSystemAudioRoute");
            RouteInfo route_info = (RouteInfo)getSystemAudioRouteMethod.invoke(media_route);

            Class mediaRouterStaticClass = Class.forName("android.media.MediaRouter$Static");
            Field staticField = mediaRouterClass.getDeclaredField("sStatic");

            Field[] array = mediaRouterStaticClass.getDeclaredFields();
            for(Field one: array){
                Log.i("CLASS_FIELD", "" + one.getName().toString());
            }
            Field normalOutputField = mediaRouterStaticClass.getDeclaredField("mSelectedRoute");
            AccessibleObject.setAccessible(new AccessibleObject[]{staticField}, true);
            AccessibleObject.setAccessible(new AccessibleObject[]{normalOutputField}, true);
            Object speakerRoute = normalOutputField.get(staticField.get(null));
            if (speakerRoute != media_route.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO)) {
                // Phone, Headphone, HDMI, etc..
                outputDeviceName = "name: " + route_info.getName().toString();
                media_route.selectRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO, route_info);
                media_route.
                outputDeviceName2 = "name: " + route_info.getName().toString();
            } else {
                // Audio is routed to A2DP
                outputDeviceName  = "name: A2DP";
                outputDeviceName2 = "name: " + route_info.getName().toString();
            }
            Log.i("outputDeviceName", ""+ outputDeviceName);
            Log.i("outputDeviceName2", ""+ outputDeviceName2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 

What can I do to route the output sound?

1

There are 1 answers

2
Mihai si atat On

I only have a comment, but I must have 50 reputation for comment.

The thing is that routing sound to the earpiece and speakers is possible when headset is plugged. You can check the app Klick in the play store. They use a pressy click that you mount into the headjack, and even though the system thinks he has a headset into the jack, you can hear the phone sound like there is not headset.

I know how to route sound to the speakers while the pressy button/headset is plugged into the jack.

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setSpeakerphoneOn(true);

BUT I don't know how they route sound to the earpiece under same conditions.I have been trying to find out that for the past week.

No luck so far, if any of you havefound a solution in the meantime please post it.

Bottom line is that it is possible and that app proves it.