如何添加在应用程序声音中静音/取消静音的切换按钮

时间:2013-03-23 23:04:21

标签: android audio toggle mute

如何在应用声音中添加将所有声音静音/取消静音的切换按钮,最好是关于如何操作的视频。

1 个答案:

答案 0 :(得分:0)

public class MyActivity extends Activity {
    private boolean _myAudioToggle;
    onCreate(Bundle savedInstanceState) {
        // ...
        _myAudioToggle = false; // audio is off, set to true to default to on
        setAudio(_myAudioToggle);
        Button button = (Button) findViewById(R.id.yourButton); // where yourButton is in your layout XML file
        button.setOnClickListener(new View.onClickListener {

            @Override
            onClick(View v) {
                _myAudioToggle = !_myAudioToggle;
                setAudio(_myAudioToggle);
            }
        });
        // ...
    }
    // ...
}

创建一个名为setAudio()的方法,它接受一个布尔值并设置音频:)

private void setAudio(boolean audioToggle) {
    // your code to toggle the audio
}