我有一个三星 Galaxy S3(目前不安全),有时我会遇到问题,例如,如果我插入耳机收听 FM 广播,然后我突然关闭应用程序并断开耳机,手机仍然认为有一个插入,所以声音(扬声器和麦克风)是耳机插孔的路由,所以当我想和某人说话时,没有声音。
不知道我解释的好不好。
当我单击一个按钮时,我想要一种将声音引导到正常状态(扬声器和麦克风)的方法。我研究了一些东西,但我什么也没得到。我认为这段代码是正确的方法,但我需要一些帮助和一些建议。
这是我的代码:
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();
}
}
我可以做些什么来路由输出声音?