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?