2

我为此苦苦挣扎了一天,但问题仍然存在。

我的活动中有 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();


}
4

1 回答 1

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);
    }
于 2012-06-06T19:57:48.703 回答