覆盖android中的后退按钮
我必须播放 mp3 文件,当单击设备上的后退按钮时,歌曲应自动停止。所以我尝试了下面给出的方法。但它不起作用。
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.audioplaying);
play=(ImageView)findViewById(R.id.play);
stop=(ImageView)findViewById(R.id.stop);
songid=(TextView)findViewById(R.id.songid);
status=(TextView)findViewById(R.id.status);
String s=Songs.song;
status.setText("Please Wait....!");
mp=new MediaPlayer();
try{
mp.setDataSource(s);
mp.prepare();
}
catch(Exception ex){
Log.e("Exception",ex.getMessage());
}
Log.e("Status","Song is going to Start");
mp.start();
start=true;
Log.e("Status","Song was Started");
status.setText("Playing...!");
songid.setText(s);
play.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
Log.d("CDA", "onKeyDown Called");
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
audioStreamer.stop();
audioStreamer.getMediaPlayer().stop();
if(start)
{
mp.stop();
start=false;
}
else{
Intent setIntent = new Intent(AudioPlay1.this,Songs.class);
startActivity(setIntent);
finish();
}
Intent setIntent = new Intent(AudioPlay1.this,Songs.class);
startActivity(setIntent);
finish();
return;
}
@Override
public void onClick(View v) {
if(v.equals(play)){
try{
mp.prepare();
}
catch(Exception ex){Log.e("Exception in onclick",ex.toString());}
mp.start();
start=true;
Log.e("Status","Song was Started again");
status.setText("Playing...!");
}
if(v.equals(stop)){
mp.stop();
start=false;
Log.e("Status","Song was stopped");
status.setText("Song was Stopped");
}
}
歌曲没有停止并且上一页无法显示。请告诉我解决方案。
此致。
先感谢您。
I have to play a mp3 file and when click on back button on the device then automatically the song should stop. So I tried below given method. But it is not working.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.audioplaying);
play=(ImageView)findViewById(R.id.play);
stop=(ImageView)findViewById(R.id.stop);
songid=(TextView)findViewById(R.id.songid);
status=(TextView)findViewById(R.id.status);
String s=Songs.song;
status.setText("Please Wait....!");
mp=new MediaPlayer();
try{
mp.setDataSource(s);
mp.prepare();
}
catch(Exception ex){
Log.e("Exception",ex.getMessage());
}
Log.e("Status","Song is going to Start");
mp.start();
start=true;
Log.e("Status","Song was Started");
status.setText("Playing...!");
songid.setText(s);
play.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
Log.d("CDA", "onKeyDown Called");
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
audioStreamer.stop();
audioStreamer.getMediaPlayer().stop();
if(start)
{
mp.stop();
start=false;
}
else{
Intent setIntent = new Intent(AudioPlay1.this,Songs.class);
startActivity(setIntent);
finish();
}
Intent setIntent = new Intent(AudioPlay1.this,Songs.class);
startActivity(setIntent);
finish();
return;
}
@Override
public void onClick(View v) {
if(v.equals(play)){
try{
mp.prepare();
}
catch(Exception ex){Log.e("Exception in onclick",ex.toString());}
mp.start();
start=true;
Log.e("Status","Song was Started again");
status.setText("Playing...!");
}
if(v.equals(stop)){
mp.stop();
start=false;
Log.e("Status","Song was stopped");
status.setText("Song was Stopped");
}
}
the song is not stopping and the previous page cant display. Please tell me the solution.
Best Regards.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否是您的问题,但是当您在 onkeydown 中调用
onBackPressed();
时,您不会返回,因此parent.onkeydown
也被调用,而“正常”的背部只是被“执行”。在那里插入一个 return 语句,这样您就不会执行父类中的正常函数。
I don't know if this is your problem, but when you call
onBackPressed();
in your onkeydown, you are not returning, so theparent.onkeydown
is also called, and the 'normal' back is just being 'executed'.Insert a return statement there so you will not do the normal function from the parent class.
用于处理所有密钥的使用
for Handleing All key use