android应用程序静音和取消静音MediaPlayer

时间:2012-06-06 19:13:48

标签: android android-activity media-player mute

我有一天在为此奋斗,问题仍然存在。

我的活动中有2个功能,它是静音和取消静音,如下所示:

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

在活动本身内,这个2功能正常。但是,当它在第一个活动中静音并且我想为下一个活动取消静音时,活动保持静音。我想知道为什么?有人可以给我一些建议吗?非常感谢!

更新: 这是代码的一部分:

 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();


}

1 个答案:

答案 0 :(得分:4)

public static AudioManager am;在一个Global.java中你可以使用和设置属性尝试它将适用于你。

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