1

How do I set the Mute option in the Android music player from source code? Please help me. This is my code. Please check it and give me your ideas.

    mute.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mute.isPressed())

                //audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

                mp.setVolume(0,0);

        else if(mute.isPressed())
                mp.setVolume(0, 1);
        }
    });     
dshapiro
  • 1,075
  • 14
  • 24
  • 1
    Don't you need braces in the if statement and else if statement i.e. if(mute.isPressed()) { mp.setVolume(0,0);} ...? – dshapiro Jun 01 '12 at 12:00
  • 1
    look at Daniel's comment, moreover your are checking for the same condition in both if and else if. – Orlymee Jun 01 '12 at 12:02
  • 1
    http://stackoverflow.com/questions/4573667/how-to-mute-mediaplayer-in-android – Thiru VT Jun 01 '12 at 12:03

2 Answers2

0
if(!mute.isPressed()) you forgot ! in else if .............


in both if it is mute.isPressed() ...........


and would be better 

    if(mute.isPressed()){
       mp.setVolume(0,0);
    }
    else{ 
      mp.setVolume(0, 1);
    }

because mute.isPressed() is a boolean if it is not true so no need to check it has to be false.........

can use code ImageView Button Toggle in Android

for you :

    final ImageView button01 = (ImageView) findViewById(R.id.button01);

    button01.setOnClickListener(new OnClickListener() {
        boolean isPressed = false;
        public void onClick(View v) {
            if (isPressed ) {
                //button01.setImageResource(R.drawable.image01);.
                 mp.setVolume(0,0);
            } else {
               //button01.setImageResource(R.drawable.image02);.
                 mp.setVolume(0,1);
            }
            isPressed = !isPressed ;
        }
    });

..........................................................................................

alternative answer :

let us make it intersting :)

button01.setOnClickListener(new OnClickListener() {
        int state = 0;
         public void onClick(View v) {
             state = (state+1)%2;
             mp.setVolume(0,state);
        }
    });

looks good ?

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • mute is image button it press means volume is mute, how can i release again the button? its again i am clicked not release please help. – user1427548 Jun 01 '12 at 12:13
  • there are various logic to main that . like 1- toggleing the tag every time mute.setTag("true")/mute.setTag("false") on onClick 2- keep a bolean and toggle in onClick 3- get which image is currently showing of mute or of unmute......... – Dheeresh Singh Jun 01 '12 at 12:22
  • use custom toggle button http://stackoverflow.com/questions/1533038/android-specify-two-different-images-for-togglebutton-using-xml – Dheeresh Singh Jun 01 '12 at 12:24
  • same problem when i am click mute is working again how can i release it. again i am pressing means sound is not enable. i declare any variable? like true and false. – user1427548 Jun 01 '12 at 12:27
  • yes because in your code it will never go in else if(mute.isPressed()), as first/main if is same as if(mute.isPressed()) so either it will go in first if or will not go in any ......... – Dheeresh Singh Jun 01 '12 at 12:29
  • please give complete code for this. i am using same image button for both. – user1427548 Jun 01 '12 at 13:05
  • is this not works ... :( .... http://stackoverflow.com/questions/10438014/imageview-button-toggle-in-android – Dheeresh Singh Jun 01 '12 at 13:08
0

It looks like you want to try

mute.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        if(mute.isPressed()){
            mp.setVolume(0,0);
        }
        else if(!mute.isPressed()){
            mp.setVolume(0, 1);
        }
    }});

The problems were that you didn't have proper bracketing and that you weren't checking the proper expression in your else if statement. Let us know how that works for you.

dshapiro
  • 1,075
  • 14
  • 24
  • same problem when i am click mute is working again how can i release it. again i am pressing means sound is not enable. i declare any variable? like true and false. – user1427548 Jun 01 '12 at 12:27
  • So, the problem is that you press mute once and the volume mutes as expected, but then you press mute again and the volume remains muted? Try using `setOnItemClickListener` instead of `setOnClickListener`. Not sure if that will fix your problem, but it's worth a try. – dshapiro Jun 01 '12 at 13:04