[英]How to stop audio that is playing when back button is pressed?
This is my code : 这是我的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer imp = MediaPlayer.create(this,R.raw.cat);
ImageButton btn = (ImageButton)findViewById(R.id.dog);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imp.start();
}
});
}
}
Use the code below: 使用以下代码:
@Override
public void onBackPressed() {
super.onBackPressed();
imp.stop();
}
Override the activity's onBackPressed(). 覆盖活动的onBackPressed()。 Something like: 就像是:
@Override
public void onBackPressed() {
super.onBackPressed();
if (imp.isPlaying()) {
imp.stop();
}
}
Try this following code to stop Audio file. 尝试使用以下代码停止音频文件。
@Override
public void onBackPressed ()
{
if (mp != null)
mp.stop();
super.onBackPressed();
}
I hope this may help you. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.