0

我在我的设置活动中有它,当用户单击 ToggleButton 时,它应该在整个应用程序中静音,但它不起作用。我在教程类中放置的 SoundPool onClick 按钮声音仍在播放声音 onClick。我已经指定了我的共享带有切换按钮的首选项。

这是我的代码,

    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"));
            }
        });









    }
4

1 回答 1

1

根据setSoundEffectsEnabled的文档

设置此视图是否应为单击和触摸等事件启用声音效果。

如果您已经播放声音,您可能希望禁用视图的声音效果,例如,播放 dtmf 音调的拨号键。

所以这个功能应该设置关闭视图的声音效果(如点击或触摸)。不设置关闭设备声音。

For your purpose check AudioManager

于 2013-03-24T04:42:33.040 回答