作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 Android 的新手,一直在在同一 Activity 中停止媒体播放器。
我想创建一个应用程序,为不同的 Activity 播放不同的音乐。
创建第一个 Activity 后,我无法停止音乐。
即使我退出我的应用程序,音乐仍在播放。
这是我的代码:
public class LayoutThreeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_three);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.layout_three, menu);
return true;
}
public void openNewActivity(View view) {
// Do something in response to button
Intent intent = new Intent(this, LayoutOneActivity.class);
startActivity(intent);
MediaPlayer mp = MediaPlayer.create(this, R.raw.song2);
mp.start();
}
最佳答案
您必须释放播放器的 OnPause 或 OnDestroy 方法。
@Override
public void onDestroy() {
if (mp!= null) mp.release();
}
关于java - 如何在 Android 的新 Activity 中停止媒体播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21662742/
我是一名优秀的程序员,十分优秀!