我的方法是(不需要外部声音,因为我将声音文件放在资源文件夹中):
在 onCreate 中:
mp = MediaPlayer.create(getBaseContext(), R.raw.sound); /*Gets your
soundfile from res/raw/sound.ogg */
mp.start(); //Starts your sound
//Continue with your run/thread-code here
请记住声音为 .ogg 格式; Android 完全支持它。
以下是关于在启动屏幕活动停止时处理声音的重要事项:
有两种通用方法可以在启动屏幕(及其内部的声音)停止时对其进行管理:
-
销毁整个活动:
protected void onStop() {
super.onStop();
ur.removeCallbacks(myRunnable); /*If the application is stopped;
remove the callback, so the next time the
application starts it shows the Splash Screen again, and also, so the
thread-code,
don't continue after the application has stopped */
finish();
onDestroy();
}
-
或者你可以在 onStop 中停止声音:
protected void onStop() {
super.onStop();
if(mp.isPlaying()){ //Must check if it's playing, otherwise it may be a NPE
mp.pause(); //Pauses the sound
ur.removeCallbacks(myRunnable);
}
}
如果您选择第二种选择,您还必须在 onStart 方法中启动回调和 MediaPlayer。
I prefer第一种选择。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)