如何在android中的启动画面播放音频

isc*_*ers 1 android

如何在启动画面期间播放音频.需要指导.

Cur*_*ain 7

我这样做的方法(不需要外部声音,因为我把我的声音文件放在我的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活动停止时处理声音的重要事项:

当停止时,有两种常规方法可以管理启动画面(及其内部的声音):

  1. 破坏整个活动:

    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)
  2. 或者您可以在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.

更喜欢第一种选择.