gpt4 book ai didi

java - 如何在 Android 的新 Activity 中停止媒体播放器

转载 作者:搜寻专家 更新时间:2023-11-01 08:00:42 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com