android application mute and unmute MediaPlayer

Yuen Tong picture Yuen Tong · Jun 6, 2012 · Viewed 8.2k times · Source

i was struggling for this for one day and the issue is still there.

I have 2 function in my activity which is mute and unmute as below:

private void mute() {
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}

public void unmute() {
      AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      am.setStreamMute(AudioManager.STREAM_MUSIC, false);
    }

Within the activity itself this 2 function work fine. However, when it is muted in the first activity and I want to unmute it for the next activity, the activity remain mute. I am wondering why? Could anyone please give me some advice? Thanks a lot!

Updated: This is part of the code:

 ImagePlay.setOnClickListener(new View.OnClickListener() 
                        {
                            // Perform button logic
                            @Override
                            public void onClick(View v)
                            {
                                unmute();
                                onResume();
                                Speaker = (ImageView)findViewById(R.id.ImageSpeaker);    
                                Speaker.setImageBitmap(mAudioImageArray[0]);
                                closeDialog.dismiss();
                                play(DOSParseActivity.this,introAudioPath);
                                isMute = false;
                            }
                        });

                        ImageMute.setOnClickListener(new View.OnClickListener() 
                        {
                            // Perform button logic
                            @Override
                            public void onClick(View v)
                            {
                                Speaker = (ImageView)findViewById(R.id.ImageSpeaker);    
                                Speaker.setImageBitmap(mAudioImageArray[1]);
                                closeDialog.dismiss();
                                mute();
                                onPause();
                                isMute = true;
                            }
                        });

                        ImageStop.setOnClickListener(new View.OnClickListener() 
                        {
                            // Perform button logic
                            @Override
                            public void onClick(View v)
                            {
                                Speaker = (ImageView)findViewById(R.id.ImageSpeaker);    
                                Speaker.setImageBitmap(mAudioImageArray[2]);
                                closeDialog.dismiss();
                                stop();
                                isMute = false;
                            }
                        });

            /////////////////////////////////////Navigation Button////////////////////////////
                        imageNext = new ImageButton(this);
                        InputStream toNextInput = mngr.open(toNextImage);
                        final Bitmap bitmaptoNext = BitmapFactory.decodeStream(toNextInput);
                        imageNext = (ImageButton)findViewById(R.id.ImageNext);
                        imageNext.setImageBitmap(bitmaptoNext);
                        imageNext.setOnClickListener(new View.OnClickListener() 
                        {
                            public void onClick(View v) 
                            {
                                stop();
                                Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmaptoNext, 54, 54, true);
                                imageNext.setImageBitmap(resizedBitmap);

                                Intent intent = new Intent(v.getContext(), Characteristic.class);

                                intent.putExtra("checkMute", "mute" );
                                intent.putExtra("languageSelection", languageSelected );
                                startActivity(intent);

                            }
                        });

private void play(Context context, String file) {

    try {
        if (IntroPlayer == null) 
        {
            this.IntroPlayer = new MediaPlayer();           
        }
        else
        {
            IntroPlayer.stop();
            IntroPlayer.reset();
        }

        AssetFileDescriptor afd = context.getAssets().openFd(file);
        IntroPlayer.setDataSource(
                afd.getFileDescriptor(),
                afd.getStartOffset(),
                afd.getLength()
            );
        afd.close();
        IntroPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        IntroPlayer.setOnPreparedListener(this);
        IntroPlayer.prepareAsync();

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }


 @Override
     public void onSwipe(int direction) {
      Intent intent = new Intent();

  switch (direction) {
  case SimpleGestureFilter.SWIPE_RIGHT :
      if (IntroPlayer != null) 
        {
             stop();  
        }
      intent.setClass(this,LocalizationAppActivity.class);
         intent.putExtra("languageSelection", languageSelected );
         break;

  case SimpleGestureFilter.SWIPE_LEFT :  
    if (IntroPlayer != null) 
    {
        stop();   
    }
      intent.setClass(this,Characteristic.class);
      intent.putExtra("languageSelection", languageSelected );

      break;

  }
   startActivity(intent);

 }

@Override
public void onPrepared(MediaPlayer mp) {
    // TODO Auto-generated method stub
     mp.start();
}

private void stop() {
    IntroPlayer.stop();
}

private void mute() {
    am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}

public void unmute() {
      int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      am.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
      am.setStreamMute(AudioManager.STREAM_MUSIC, false);
    }

@Override
public void onDestroy() {
     super.onDestroy();
     stop();


}

Answer

Parag Chauhan picture Parag Chauhan · Jun 6, 2012

Take public static AudioManager am ; in one Global.java and u can use and set property try it will be work for u.

private void mute() {
    Global.am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}

public void unmute() {
      Global.am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      am.setStreamMute(AudioManager.STREAM_MUSIC, false);
    }