如何检查和重定向有线耳机和免提电话的声音? [英] How to check and redirect audio between wired headset and speaker phone?

查看:254
本文介绍了如何检查和重定向有线耳机和免提电话的声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个简单的应用程序,这应该的切换的扬声器电话和有线耳机之间的一个按钮单击事件播放音频。我试图使用的 isWiredHeadsetOn()的功能,但它说,这是pcated为Android API拉特5起去$ P $。那么,如何检查,如果当前的音频播放或不通过有线耳机,这样我可以把它重定向到手机音箱?

I am developing a simple app , that should toggle between speaker phone and wired headset to play audio on a button click event. I am trying to make use of isWiredHeadsetOn() function, but it says that this is deprecated for Android API lvl 5 onwards. So how do I check if currently audio is playing or not through wired headset so that I can redirect it to the phone speaker?

注意:我开始我的应用程序与插入到3.5毫米插孔的手机耳机

Note: I start my app with the headphone plugged to the 3.5mm jack of the phone.

这是我尝试在code到目前为止:

This is my attempt at the code so far:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audio_re_direct);
    redirect = (Button)findViewById(R.id.redirect);
    final AudioManager audio =(AudioManager)getApplicationContext().getSystemService(AUDIO_SERVICE);

    redirect.setOnClickListener(new View.OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(audio.isWiredHeadsetOn())
            {
                audio.setWiredHeadsetOn(false);
                audio.setSpeakerphoneOn(true);
                Toast.makeText(getApplicationContext(), "SpeakerPhone On", Toast.LENGTH_LONG).show();
                redirect.setText("Turn on headset");

            }
            else 
            {
                audio.setSpeakerphoneOn(false);
                audio.setWiredHeadsetOn(true);
                Toast.makeText(getApplicationContext(), "Wired Headset On", Toast.LENGTH_LONG).show();
                redirect.setText("Turn off headset");
            }

            }

    });
        }

但应用程序不会触发的。最初,它检测到有线耳机为present,显示敬酒消息的打开扬声器的,就是这样。它并不在两者之间切换。

But the app is not toggling at all. Initially it detects that the wired headset is present, shows the Toast message SpeakerPhone On and that's it. It does not toggle between the two.

有人请帮助我,使这项工作。谢谢你。

Someone please help me to make this work. Thanks.

推荐答案

终于成功地解决各地用这个工作我自己的问题(所建议的@noelicus):

Finally managed to solve my own problem using this work around (as suggested by @noelicus):

<一个href="http://stackoverflow.com/questions/20965530/how-to-mute-audio-in-headset-but-let-it-play-on-speaker-programmatically">How静音音频耳机,但让它以编程扬声器上播放?

发布,以供参考,以其他人谁可能会卡住我一样。 :)

Posted for reference to others who might get stuck like me. :)

这篇关于如何检查和重定向有线耳机和免提电话的声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆