2013-03-24 47 views
0

我的設置活動中,當用戶單擊ToggleButton時,它應該將整個應用程序中的聲音靜音,但它不工作。SoundPool onClick按鈕聽起來像我放在我的教程類中仍然在播放聲音onClick.I已經使用ToggleButton指定了我的共享首選項。打開/關閉聲音按鈕不起作用

這裏是我的代碼,

package com.fullfrontalgames.numberfighter; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.media.AudioManager; 
import android.media.SoundPool; 
import android.os.Bundle; 
import android.preference.Preference; 
import android.preference.PreferenceManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.ToggleButton; 


public class Settings extends Activity { 











    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.settings); 


     Button Notifications = (Button) findViewById(R.id.Notifications); 
     Button Done = (Button) findViewById(R.id.done); 
     Button AccountSettings = (Button) findViewById(R.id.AccountSettings); 
     final ToggleButton AT = (ToggleButton) findViewById(R.id.AudioToggle); 



     AT.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       SharedPreferences appPrefs = 
         getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences", 
           MODE_PRIVATE); 
       SharedPreferences.Editor editor = appPrefs.edit(); 
       editor.putBoolean("atpref", AT.isChecked()); //value to store 
       editor.commit(); 

       if ((AT.isChecked())) { 
        AT.setSoundEffectsEnabled(true); 

        } else { 
        AT.setSoundEffectsEnabled(false); 
         } 
       } 

     }); 







     SharedPreferences appPrefs = 
       getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences", 
       MODE_PRIVATE); 
     boolean atpref = appPrefs.getBoolean("atpref", true); //default is true 
     AT.setChecked(atpref); 
     Done.setOnClickListener(new View.OnClickListener() { 








      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent Intent = new Intent(Settings.this,activity_main.class); 
       Intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(Intent); 



      } 
     }); 
     Notifications.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startActivity(new Intent("com.fullfrontalgames.numberfighter.Notifications")); 
      } 
     }); 
     AccountSettings.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startActivity(new Intent("com.fullfrontalgames.numberfighter.AccountSettings")); 
      } 
     }); 









    } 
+0

想方設法把使用該值at.ischecked()剛過sharedpreferences調試,我不覺得現在有任何問題。嘗試這個 – 2013-03-24 04:40:12

+0

你的代碼看起來OK嘗試使用'Log'值或'Toast' – 2013-03-24 04:42:21

回答

1

根據setSoundEffectsEnabled

設置這種觀點是否應該對事件 如點擊和觸摸啓用聲音效果doc

如果您已經播放了 聲音,例如,播放dtmf音調的撥號鍵,您可能希望禁用視圖的聲音效果。

因此,此功能應設置爲關閉視圖的聲音效果(如點擊或觸摸)。不要關閉設備聲音。

你的目的檢查AudioManager

+0

謝謝,我想這是這樣的,我會查找AudioManager爲此! – Cranosaur 2013-03-24 04:48:53

+1

搜索靜音取消靜音設備在所以..我敢打賭,你會很快找到解決方案 – stinepike 2013-03-24 04:53:57

+0

setSoundEffectsEnabled我已經把它設置爲false我的一個按鈕,但沒有效果。它仍然會發出聲音。在Google Nexus 10 – 2014-01-01 12:02:58