0

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?

Víctor Martín
  • 3,352
  • 7
  • 48
  • 94
  • An app can't tell the system that a headset has been detached. If the system thinks the headset still is attached, you're out of luck. You could try re-attaching it and then detaching it again and see if that helps. – Michael Dec 21 '13 at 11:11
  • This is not true at all. A while ago, I made a similar application for Gingerbread (redirect output), but the methods for doing this in Jelly Bean have changes, so old no longer works. :( And I think this was the way to do the same. :( – Víctor Martín Dec 21 '13 at 18:10
  • Well, I maintain that an app can't tell the audio HAL (which is where the routing decisions are made) that a headset has been detached. There are ways that you can tell it e.g. that you'd like to have some types of audio routed to the loudspeaker _even if_ a headset is attached (which may be what you're thinking of), but that starts getting tricky or even impossible (in particular for inputs) if you want to cover all apps and all possible use-cases. Or maybe you were thinking of the now-deprecated `setRouting` method? – Michael Dec 21 '13 at 18:40
  • Yes, in the old app I used setRouting. But now is deprecated :( – Víctor Martín Dec 22 '13 at 09:32
  • Maybe you should ask yourself why it is that you should be writing this app in the first place? It sounds to me like your phone or Samsung's Android ROM is defective. Instead of trying to create a SW workaround for a defective product you probably should be looking for an update of the Android version on you phone, or see if you can have the phone replaced altogether. – Michael Dec 22 '13 at 09:44
  • It no longer has warranty. So I have to find a solution by my self. Anyway, thanks for your answers. – Víctor Martín Dec 22 '13 at 11:03

1 Answers1

2

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.

Mihai si atat
  • 170
  • 2
  • 13
  • After some more research I managed to reroute the sound to the speakers even when headset/headphones/or a pressy click is plugged into the jack. – Mihai si atat Jul 13 '14 at 16:22
  • Editing my above answer is a pane, I keep getting formatting errors and I cant spent my entire day understanding what is the problem. – Mihai si atat Jul 13 '14 at 16:35