简体   繁体   English

MediaRoute和路由音频

[英]MediaRoute and routing audio

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. 我有三星Galaxy S3(目前没有安全保护),我有时会遇到问题,例如,如果我插入耳机收听FM收音机,突然关闭应用程序并断开耳机,手机仍然相信有一个插入,所以声音(扬声器和麦克风)是通往耳机插孔的路径,所以当我想和别人说话时,没有声音。

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? 如何路由输出声音?

I only have a comment, but I must have 50 reputation for comment. 我只有一个评论,但我必须有50个评论的声誉。

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. 您可以在Play商店中查看应用Klick。 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. 他们使用你安装在headjack中的压力点击,即使系统认为他有一个耳机插入插孔,你可以听到手机声音,就像没有耳机。

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. 最重要的是,这是可能的,并且该应用证明了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM