gpt4 book ai didi

java - 选择背景音乐何时停止

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:30 24 4
gpt4 key购买 nike

我正在编写一个带有背景音乐的小游戏,但是每当我关闭程序时它都会继续播放,因此我决定尝试在应用程序关闭时停止该播放器,并在切换到另一个 Activity 时继续播放。

但是这段代码会使我的游戏崩溃。

我必须做什么?

Java代码如下:

package com.example.caca_a_frase;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Menu extends Activity {

private static Button jogar;
private static Button jogar2;
MediaPlayer player;
public static boolean shouldPlay = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_menu);

//Isto faz a música tocar quando carrega a atividade!
player = MediaPlayer.create(this, R.raw.background_music);
player.setLooping(true);
player.setVolume(10f,10f);
player.start();
//Até aqui!

onClickButtonListener();
onClickButtonListener2();

findViewById(R.id.fullscreen_content_controls);
findViewById(R.id.fullscreen_content);
}

//Botão para jogar!

public void onClickButtonListener() {
jogar = (Button) findViewById(R.id.botaoJogar);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.click);
jogar.setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
mp2.start();
Intent intent = new Intent("com.example.caca_a_frase.Jogo");
startActivity(intent);
}
}
);

//Acaba aqui!
//Botão para saír!
Button sair = (Button) findViewById(R.id.botaoSair);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
sair.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

mp.start();
createDialog();
}
private void createDialog() {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(Menu.this);
alertDlg.setMessage("Tens a certeza que queres saír?");
alertDlg.setCancelable(false);

alertDlg.setPositiveButton("Sim", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);

}
});

alertDlg.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {


}
});

alertDlg.create().show();

}
});

}
//Acaba aqui!
//Botão para Opções!

public void onClickButtonListener2() {
jogar2 = (Button) findViewById(R.id.botaoOpcoes);
final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.click);
jogar2.setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
mp3.start();
Intent intent = new Intent("com.example.caca_a_frase.Opcoes");
startActivity(intent);
}
}
);
}

//Acaba aqui!
//Quando carrega voltar.

@Override
public void onBackPressed() {
createDialog();
}
private void createDialog() {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(Menu.this);
alertDlg.setMessage("Tens a certeza que queres saír?");
alertDlg.setCancelable(false);

alertDlg.setPositiveButton("Sim", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);

}
});

alertDlg.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {


}
});

alertDlg.create().show();

}
//Acaba aqui!
//Quando aplicação é fechada!
public void onStop() {
super.onStop();
if (shouldPlay == false) { // it won't pause music if shouldPlay is true
player.pause();
}
}
public void onResume() {
if (shouldPlay == false) { // it won't pause music if shouldPlay is true
player.start();
}
}
//Acaba aqui!
}

最佳答案

我设法自己解决了错误,它在这里:

public void onStop() {
super.onStop();

if (shouldPlay == false) { // it won't pause music if shouldPlay is true
player.pause();
}
}

public void onResume() {
if (shouldPlay = false) { // it won't pause music if shouldPlay is true
player.start();
}
}

onPause()onResume()不应该是public void,它们应该是protected void,这就是游戏崩溃的问题。

这是现在可以运行的代码:

package com.example.caca_a_frase;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Menu extends Activity {

private static Button jogar;
private static Button jogar2;
MediaPlayer player;
public static boolean shouldPlay = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_menu);

//Isto faz a música tocar quando carrega a atividade!
player = MediaPlayer.create(this, R.raw.background_music);
player.setLooping(true);
player.setVolume(10f,10f);
player.start();
//Até aqui!

onClickButtonListener();
onClickButtonListener2();

findViewById(R.id.fullscreen_content_controls);
findViewById(R.id.fullscreen_content);
}

//Botão para jogar!

public void onClickButtonListener() {
jogar = (Button) findViewById(R.id.botaoJogar);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.click);
jogar.setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
mp2.start();
Intent intent = new Intent("com.example.caca_a_frase.Jogo");
shouldPlay = true;
startActivity(intent);
}
}
);

//Acaba aqui!
//Botão para saír!
Button sair = (Button) findViewById(R.id.botaoSair);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
sair.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

mp.start();
createDialog();
}
private void createDialog() {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(Menu.this);
alertDlg.setMessage("Tens a certeza que queres saír?");
alertDlg.setCancelable(false);

alertDlg.setPositiveButton("Sim", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);

}
});

alertDlg.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {


}
});

alertDlg.create().show();

}
});

}
//Acaba aqui!
//Botão para Opções!

public void onClickButtonListener2() {
jogar2 = (Button) findViewById(R.id.botaoOpcoes);
final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.click);
jogar2.setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
mp3.start();
Intent intent = new Intent("com.example.caca_a_frase.Opcoes");
shouldPlay = true;
startActivity(intent);
}
}
);
}

//Acaba aqui!
//Quando carrega voltar.

@Override
public void onBackPressed() {
createDialog();
}
private void createDialog() {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(Menu.this);
alertDlg.setMessage("Tens a certeza que queres saír?");
alertDlg.setCancelable(false);

alertDlg.setPositiveButton("Sim", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);

}
});

alertDlg.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {


}
});

alertDlg.create().show();

}
//Acaba aqui!
//Quando aplicação é fechada!
@Override
protected void onPause() {
super.onPause();
if (shouldPlay == false){
player.pause();
}
}
@Override
protected void onResume() {
super.onResume();
if (shouldPlay == false){
player.start();
}
else {
shouldPlay = false;
}
}
//Acaba aqui!
}

关于java - 选择背景音乐何时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003310/

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