通过这段代码,我可以在后台运行歌曲,单击“是”按钮并以“否”停止,但它只能运行一次。如果我再次单击“是”按钮(第二次),应用程序将停止。所以需要一个帮助,我希望我的活动能够在每次点击时按照这些警告对话框的按钮运行和停止音乐。
public class Music extends Activity {
final Context context = this;
MediaPlayer song;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder
.setMessage("Turn ON the music?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
song.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
song = MediaPlayer.create(Music.this, R.raw.fing);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
try {
song.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
song.start();
}
// dialog.cancel();
}
)
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog,int id){
// if this button is clicked, just close
// the dialog box and do nothing
if(song!=null && song.isPlaying()){
song.stop();
dialog.cancel();
}
}} );
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();}}
答案 0 :(得分:1)
您的媒体播放器的所有初始化(例如create,setAudioStreamType,prepare)实例都可以放入onCreate方法而不是onClick对话框。在onClick中只需调用停止和播放方法。
喜欢这个
public class Music extends Activity {
final Context context = this;
MediaPlayer song;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
song.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
song = MediaPlayer.create(Music.this, R.raw.fing);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
try {
song.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder
.setMessage("Turn ON the music?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
song.start();
}
// dialog.cancel();
}
)
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog,int id){
// if this button is clicked, just close
// the dialog box and do nothing
if(song!=null && song.isPlaying()){
song.stop();
dialog.cancel();
}
}} );
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
答案 1 :(得分:1)
使用此
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
song.reset(); // this makes sure that the song object was not assigned before (it recreates the MediaPlayer)
song.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
song = MediaPlayer.create(Music.this, R.raw.fing);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
try {
song.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
song.start();
}
// dialog.cancel();
}
)
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog,int id){
// if this button is clicked, just close
// the dialog box and do nothing
if(song!=null && song.isPlaying()){
song.stop();
song.release(); // This releases the song object so you can initiate another MediaPlayer object if you want.
dialog.cancel();
}
}} );
这会重置MediaPlayer对象以确保你有一个新的对象,如果音乐停止播放它会释放MediaPlayer(它也会释放RAM内存)
答案 2 :(得分:0)
尝试致电
if (song !=null){
song.release()}
前
song = MediaPlayer.create(Music.this, R.raw.fing);
你能显示你的日志吗?
答案 3 :(得分:0)
我解决了在setOnPreparedListener中调用song.start()的问题,任何在此方法之外启动的调用都会产生错误。