本文介绍了仅将我的应用程序的背景音乐静音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个应用程序,我想在主菜单中创建一个按钮来控制用户是否只想对此应用程序(而非设备)使用音乐(取消静音)或不使用(静音),并播放背景音乐在另一种布局上.

i build an app, and in main menu i want to create a button to control whether the user wants to use music (unmute) or not (mute) only for this application (not the device) and the background music is played on another layout..

这是我调用媒体播放器的方法(在Question.java类中):

This is my method to call media player (in class Question.java):

public void playSound(int arg)
{
    try
    {
        if(player != null)
        {
            if (player.isPlaying())
            {
                player.stop();
                player.reset();
                player.release();
            }
        }
    }
    catch(Exception e)
    {

    }

    if (arg == 2)
    {
        player = MediaPlayer.create(this, R.raw.b);
    }

    if(player != null)
    {
        player.setLooping(true);
        player.start();
    }
}

这是按钮的代码(在我的主菜单MainActivity.java中):

And this is the code for the button (in my main menu, MainActivity.java):

public class MainActivity extends Activity
{
  //another code.....
  public String klik;

  protected void onCreate(Bundle savedInstanceState)
  {
    //another code...

    DataAdapter myDbHelper = new DataAdapter(this);
    myDbHelper.createDatabase();
    myDbHelper.open();
    Cursor get = myDbHelper.getSound(1);
    klik = Utility.GetColumnValue(get, "klik");
    //to get value of klik on my database

    if(klik.equals("1"))
    {
        setGbrSound(1);
        //set button's background to mute
    }
    else if(klik.equals("2"))
    {
        setGbrSound(2);
        //set button's background to unmute
    }

    myDbHelper.close();

    //another code...

    btnsuara.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            if(klik.equals("1"))
            {
                AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
                aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
                setSound(2); //update klik value in database to klik=2
                setGbrSound(2); //set button's background to unmute
                setSound(3); //change value of String klik in this class from the value of klik in database
            }
            else if (klik.equals("2"))
            {
                AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
                aManager.setStreamMute(AudioManager.STREAM_MUSIC, false);

                setSound(1); //update klik value in database to klik=1
                setGbrSound(1); //set button's background to mute
                setSound(3); //change value of String klik in this class from the value of klik in database
            }
        }
    });
}

如果我在MainActivity.java中使用此代码,则该功能正常工作(我认为是因为此类中没有媒体播放器可播放).在运行Question.java类时,将调用playSound方法并播放媒体播放器.当我返回MainActivity.java并选择使音乐静音时,再次进入Question.java时,不播放背景音乐,然后禁用了设备的媒体音量"设置(不仅针对我的应用程序)).有人知道如何解决这个问题吗?谢谢..

if i use this code in MainActivity.java, the function is working properly (i think it is because no media player to be played in this class). When the class Question.java is running, the method playSound is called and the media player is played. When i go back to the MainActivity.java and i choose to mute the music, and i go to the Question.java again, the background music is not played and then the Media volume setting for the device is disabled (not only for my application).Anyone know how to solve this? Thx..

我试图使用此代码,但仍然不能将其静音..

i tried to use this code but it is still cannot be unmuted..

    btnsuara.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            if(isMuted(mContext)==false)
            {
                AudioManager aManager =(AudioManager)getSystemService(AUDIO_SERVICE);
                aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);

                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
                Editor editor = prefs.edit(); // get an Editor object
                editor.putBoolean("isMuted", true); // set the mute boolean to true (mute)
                editor.commit();

                setGbrSound(2); //set button's background to unmute/sound on
            }
            else if (isMuted(mContext)==true)
            {
                AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
                aManager.setStreamMute(AudioManager.STREAM_MUSIC, false);

                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
                Editor editor = prefs.edit(); // get an Editor object
                editor.putBoolean("isMuted", false); // set the mute boolean to false (unmute)
                editor.commit();

                setGbrSound(1); //set button's background to mute/sound off
            }
        }
    });

public static boolean isMuted(Context c)
{
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
     return prefs.getBoolean("isMuted", false); // false is the default value
}

有任何评论吗?

推荐答案

最快的方法:

1).将值另存为boolean或int或 SharedPreferences

1). Save the value as boolean or int or whatever in SharedPreferences

2).在应用中可以播放音乐的任何位置检查此值

2). Check this value from anywhere in your app where you play music

您完成了.询问是否需要一些示例代码

You are done. Ask if you need some sample code

编辑示例:

在菜单 Activity 中,将 Context 定义为 class字段(在那里您声明了所有其他字段):

In your menu Activity, define a Context as a class field (where you have all the other fields declared):

     private Context mContext = this;

1).然后(在您的 onClick()方法中)设置一个布尔值,如:

1). Then (in your onClick() method), set a boolean like:

   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
   Editor editor = prefs.edit(); // get an Editor object
   editor.putBoolean("isMuted", true); // set the mute boolean to true
   editor.commit(); // save the changes

2).使用此方法(通过有效的 Context )可在您应用程序的任何位置执行检查.您可以在活动"中定义并调用此方法,从中检查静音"选项是否已激活:

2). Perform the check from anywhere in your app by using this method (pass a valid Context). You can define and call this method in the Activity from which you want to check if the "mute" option was activated:

    public static boolean isMuted(Context c){
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
     return prefs.getBoolean("isMuted", false); // false is the default value
      }

请注意,您可以再次使用 Editor.commit()方法

Note that you can overwrite this value by using Editor.commit() method again

这篇关于仅将我的应用程序的背景音乐静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:01