我的 android 应用程序的 res/raw 文件夹中有一个小 (200kb) mp3。我正在尝试在 Eclipse 的模拟器中运行它。它被识别为 R 文件中的资源,但是当我尝试准备/启动时,我的 Activity 崩溃了!还有什么我需要改变的,也许是在 list 中?
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile); try { mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { // handle this later }
最佳答案
在启动 Activity 时,即 onCreate 输入以下代码。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();
}
当停止 Activity 时,即 onDestroy 输入以下代码。
public void onDestroy() {
mPlayer.stop();
super.onDestroy();
}
希望对你有帮助:)
关于android - 如何在我的 android 应用程序的 res/raw 文件夹中播放 mp3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4162230/