我这样做的方法(不需要外部声音,因为我把我的声音文件放在我的resources-folder中):
在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
Run Code Online (Sandbox Code Playgroud)
记得以.ogg格式发出声音; 在Android中完全支持它.
关于Splash Screen活动停止时处理声音的重要事项:
当停止时,有两种常规方法可以管理启动画面(及其内部的声音):
破坏整个活动:
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();
}
Run Code Online (Sandbox Code Playgroud)或者您可以在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);
}
}
Run Code Online (Sandbox Code Playgroud)如果选择第二种方法,还必须在onStart方法中启动Callback和MediaPlayer.
我更喜欢第一种选择.
| 归档时间: |
|
| 查看次数: |
8544 次 |
| 最近记录: |