gpt4 book ai didi

java - Android 媒体播放器双重播放问题

转载 作者:行者123 更新时间:2023-12-01 09:18:39 25 4
gpt4 key购买 nike

我正在做井字游戏。

我有一个管理音乐的 MusicManager 类和一个基于 6 个主题启动音乐的 ThemesActivity 类。每当单击按钮时,它都会按预期停止其他媒体播放器。但是,当双击主题时,它会播放音乐两次。我尝试使用 isPlaying() 来阻止它,但没有成功。

下面是 MusicManager 的以下代码:

//Mediaplayer -- For All 6 themes
private MediaPlayer mp;
public static MediaPlayer normalMP;
public static MediaPlayer loveMP;
public static MediaPlayer tomAndJerryMP;
public static MediaPlayer clashOfClansMP;
public static MediaPlayer cricketMP;
public static MediaPlayer iceAgeMP;

public static void playMusic(Context context) {

int chosenTheme = Utils.loadPreferences(context, "theme", 0);

if(chosenTheme == 0){
normalMP = MediaPlayer.create(context, R.raw.darude_sandstorm);
normalMP.setLooping(true);
normalMP.start();

if(loveMP != null) {
loveMP.release();
}
if(tomAndJerryMP != null) {
tomAndJerryMP.release();
}
if(clashOfClansMP != null) {
clashOfClansMP.release();
}
if(cricketMP != null) {
cricketMP.release();
}
if(iceAgeMP != null) {
iceAgeMP.release();
}
}

if(chosenTheme == 1){
loveMP = MediaPlayer.create(context, R.raw.love);
loveMP.setLooping(true);
loveMP.start();

if(normalMP != null) {
normalMP.release();
}
if(tomAndJerryMP != null) {
tomAndJerryMP.release();
}
if(clashOfClansMP != null) {
clashOfClansMP.release();
}
if(cricketMP != null) {
cricketMP.release();
}
if(iceAgeMP != null) {
iceAgeMP.release();
}
}

if(chosenTheme == 2){
tomAndJerryMP = MediaPlayer.create(context, R.raw.tommy);
tomAndJerryMP.setLooping(true);
tomAndJerryMP.start();

if(normalMP != null) {
normalMP.release();
}
if(loveMP != null) {
loveMP.release();
}
if(clashOfClansMP != null) {
clashOfClansMP.release();
}
if(cricketMP != null) {
cricketMP.release();
}
if(iceAgeMP != null) {
iceAgeMP.release();
}
}

if(chosenTheme == 3){
clashOfClansMP = MediaPlayer.create(context, R.raw.pinkpanther);
clashOfClansMP.setLooping(true);
clashOfClansMP.start();

if(normalMP != null) {
normalMP.release();
}
if(loveMP != null) {
loveMP.release();
}
if(tomAndJerryMP != null) {
tomAndJerryMP.release();
}
if(cricketMP != null) {
cricketMP.release();
}
if(iceAgeMP != null) {
iceAgeMP.release();
}
}

if(chosenTheme == 4){
cricketMP = MediaPlayer.create(context, R.raw.cricket);
cricketMP.setLooping(true);
cricketMP.start();

if(normalMP != null) {
normalMP.release();
}
if(loveMP != null) {
loveMP.release();
}
if(tomAndJerryMP != null) {
tomAndJerryMP.release();
}
if(clashOfClansMP != null) {
clashOfClansMP.release();
}
if(iceAgeMP != null) {
iceAgeMP.release();
}
}

if(chosenTheme == 5) {
iceAgeMP = MediaPlayer.create(context, R.raw.ice_age);
iceAgeMP.setLooping(true);
iceAgeMP.start();

if (normalMP != null) {
normalMP.release();
}
if (loveMP != null) {
loveMP.release();
}
if (tomAndJerryMP != null) {
tomAndJerryMP.release();
}
if (clashOfClansMP != null) {
clashOfClansMP.release();
}
if (cricketMP != null) {
cricketMP.release();
}
}
}

public static void stopMusic(Context context) {

int chosenTheme = Utils.loadPreferences(context, "theme", 0);

if(chosenTheme == 0){
normalMP.stop();
}
if(chosenTheme == 1){
loveMP.stop();
}
if(chosenTheme == 2){
tomAndJerryMP.stop();
}
if(chosenTheme == 3){
clashOfClansMP.stop();
}
if(chosenTheme == 4){
cricketMP.stop();
}
if(chosenTheme == 5){
iceAgeMP.stop();
}

}

这是我的 ThemesActivity - 音乐播放部分

//Layout Global Variables
RelativeLayout normalLayout;
RelativeLayout loveLayout;
RelativeLayout tomAndJerryLayout;
RelativeLayout clashOfClansLayout;
RelativeLayout cricketLayout;
RelativeLayout iceAgeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_themes);

//Referencing Views with IDs
normalLayout = (RelativeLayout) findViewById(R.id.normalLayout);
loveLayout = (RelativeLayout) findViewById(R.id.loveLayout);
tomAndJerryLayout = (RelativeLayout) findViewById(R.id.tom_and_jerry_layout);
clashOfClansLayout = (RelativeLayout) findViewById(R.id.clashLayout);
cricketLayout = (RelativeLayout) findViewById(R.id.cricketLayout);
iceAgeLayout = (RelativeLayout) findViewById(R.id.ice_age_layout);


normalLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MusicManager.playMusic(getBaseContext());
}

loveLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MusicManager.playMusic(getBaseContext());
}

tomAndJerryLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MusicManager.playMusic(getBaseContext());
}

clashOfClansLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MusicManager.playMusic(getBaseContext());
}

cricketLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MusicManager.playMusic(getBaseContext());
}

iceAgeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MusicManager.playMusic(getBaseContext());
}
}

最佳答案

对于六种情况,只需在播放声音之前调用此方法即可:

public static void playMusic(Context context, RelativeLayout normalLayout) {

int chosenTheme = Utils.loadPreferences(context, "theme", 0);

switch(chosenTheme){
case 0 : normalMP = MediaPlayer.create(context, R.raw.darude_sandstorm);
normalMP.setLooping(true);
normalMP.start();
normalMP.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { //Handling the resource release part

@Override
public void onCompletion(MediaPlayer mp)
{
mp.release();
normalLayout.setEnabled(true); //Enable the button after sound has played
}
});
break;
//write case statements for other five cases

}
}

双击问题:要处理多击问题,只需禁用 onclick 开头的按钮并在声音播放完毕后启用它即可。

 normalLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
normalLayout.setEnabled(false);
MusicManager.playMusic(getBaseContext(),normalLayout);
}

关于java - Android 媒体播放器双重播放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40326067/

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