Android 安卓媒体播放器没有';第二次不工作了
有了这段代码,我可以在后台运行歌曲,单击“是”按钮并用“否”停止,但它只运行一次。如果我再次单击“是”按钮(第二次),应用程序将停止。因此,需要帮助,我希望我的“我的活动”能够在每次单击这些警报对话框的按钮时以一致的方式运行和停止音乐Android 安卓媒体播放器没有';第二次不工作了,android,Android,有了这段代码,我可以在后台运行歌曲,单击“是”按钮并用“否”停止,但它只运行一次。如果我再次单击“是”按钮(第二次),应用程序将停止。因此,需要帮助,我希望我的“我的活动”能够在每次单击这些警报对话框的按钮时以一致的方式运行和停止音乐 public class Music extends Activity { final Context context = this; MediaPlayer song; @Override protected void onCreate(Bundle sa
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();}}
您可以将媒体播放器的所有初始化(如创建、设置音频流类型、准备)实例放在onCreate方法中,而不是对话框的onClick中。在onClick中,只需调用stop和play方法 像这样
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();
}
}
用这个
.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内存)尝试调用
if (song !=null){
song.release()}
以前
song = MediaPlayer.create(Music.this, R.raw.fing);
你能显示你的日志吗?我解决了在setOnPreparedListener中调用song.start()的问题,任何在此方法之外的调用都会产生错误。尝试在
onCreate
中的PositiveButton
justsong.start()中分配和设置audioMediaPlayer歌曲
代码>@Waki我一开始试过,但只试过一次;如果我在第一次播放歌曲时单击“是”,如果我在第一次播放歌曲时单击“否”,则歌曲停止播放,如果我在第二次播放歌曲时再次单击“是”,则应用程序停止播放。所以我使用了try/catch,但效果不太好。你不理解我,setAudioStreamType
,MediaPlayer.create
和prepare
也把onCreate
放在PositiveButton
onClick
只是song.start()代码>这也不起作用。不介意朋友,但这些都不起作用。我想我需要用线程来解决这个问题。