简体   繁体   English

如何在Android中实现静音功能?

[英]How to realize the mute function in android?

I want to realize the mute effect under the condition of mobile phone without root. 我想在无根手机的情况下实现静音效果。

private AudioManager myAudioManager = null;

        myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

        int ringerMode  = myAudioManager.getRingerMode();       
        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

        myAudioManager.setRingerMode(ringerMode); 

I test the above method. 我测试了上面的方法。 It has the effect on some mobile. 它对某些手机有影响。 And other mobile has no effect. 和其他手机没有作用。 Why? 为什么?

What other way to mute except this? 除此以外,还有什么其他静音方法?

There may be version sdk issue. 可能有SDK版本问题。 By the way there are several streams in Android, which one do you want to mute? 顺便说一下,Android中有多个流,您要静音哪个? Try this code: 试试这个代码:

    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // mute music stream
    audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0); // mute ring stream
    if (Build.VERSION.SDK_INT >= 8) {
        audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        audioManager.requestAudioFocus(null, AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN);
    }

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

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