我是Android编程新手。我有一个mp3文件播放,但按下后退按钮退出程序时不会停止播放。我有全局的MediaPlayer,不应该在后面的密钥代码中拾取吗?任何帮助表示赞赏。
public class JabberWockyActivity extends Activity {
WebView myWebView;
Button button1;
Button button2;
MediaPlayer mpAudio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jabber_wocky);
mpAudio=MediaPlayer.create(this, R.raw.yah3);
mpAudio.start();
mpAudio.setLooping(true);
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.jabberwocky.com/carroll/jabber/jabberwocky.html");
button1 = (Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("https://en.wikipedia.org/wiki/Jabberwocky"));
startActivity(browserIntent);
}
});
button2 = (Button)this.findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://goodvibeblog.com/wp-content/uploads/2010/06/jabberwocky2.jpg"));
startActivity(browserIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.jabber_wocky, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
mpAudio.stop();
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:1)
您可以在OnDestroy()方法中调用release()。
@Override
public void onDestroy(){
super.onDestroy();
mp.release();
}