gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:49 26 4
gpt4 key购买 nike

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

我的 Activity 中有 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);
}

在 Activity 本身中,这 2 个功能工作正常。但是,当它在第一个 Activity 中静音并且我想为下一个 Activity 取消静音时,该 Activity 保持静音。我想知道为什么?谁能给我一些建议?非常感谢!

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

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


}

最佳答案

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

关于android 应用程序静音和取消静音 MediaPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10920639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com