Disable sound just for one application

梦想的初衷 提交于 2019-12-06 15:59:49

Actually, if you want to disable a sound in your application - you can just don't play it. You can store the flags for each sound you want to play in the SharedPreferences and check it every time you want to play a sound. It's really easy!

You can probably write your own MyButton class that extends Button and override onClick.

Safvan 7
  1. Declare a string

    String strSound;

  2. enter this line in your shared preference settings

    strSound = prefs.getString("sound", "on");

  3. enter this line in your onActivityResult

    strSound = prefs.getString("sound", "on");

  4. put this lines in your media player which is used to give sounds for your application

    private void playSound()
    {
    mp = MediaPlayer.create(MyActivity.this, R.raw."your_soundfile_name");
    
    mp.setOnCompletionListener(new OnCompletionListener() 
    {
        @Override
        public void onCompletion(MediaPlayer mp) 
        {
            mp.release();
        }
    }); 
    if(strSound.equals("on"))
    mp.start();
    
  5. now put this lines on your toggle button activity

    your_togglebutton_id.setOnCheckedChangeListener(new     CompoundButton.OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            prefs = getSharedPreferences(prefName, MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            if (isChecked)
            {
                editor.putString("sound", "on");
                editor.commit();
            }
            else
            {
                editor.putString("sound", "off");
                editor.commit();
            }
        }
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!