如何在启动画面期间播放音频。 需要指导。
最佳答案
我这样做的方式(不需要外部声音,因为我把我的声音文件放在我的资源文件夹中):
在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 完全支持它。
下面是关于在 Splash Screen Activity 停止时处理声音的重要事项:
有两种通用的方法来管理停止时的启动画面(以及其中的声音):
销毁整个 Activity :
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。
我更喜欢第一种选择。
关于android - 如何在android的启动画面中播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3370211/