2013-10-02 59 views
0

我已經添加了一個5秒的聲音的MP3到我的應用程序在啓動畫面,但其播放波動時加載的應用程序,我需要做什麼流暢播放?飛濺的聲音波動或打破

public class Splash extends Activity{ 
    MediaPlayer ourSong; 
    @Override 
    protected void onCreate(Bundle TravisLoveBacon) { 
     // TODO Auto-generated method stub 
     super.onCreate(TravisLoveBacon); 
     setContentView(R.layout.splash); 
     ourSong = MediaPlayer.create(Splash.this, R.raw.onkar); 
     ourSong.start(); 
     Thread timer = new Thread(){ 
      public void run(){ 
       try{ 
        sleep(4000); 
       } catch (InterruptedException e){ 
        e.printStackTrace(); 
       }finally{ 
        Intent openStartingPoint = new Intent("com.sport.sport.MAINLAUNCHER2"); 
        startActivity(openStartingPoint); 
       } 
      } 
     }; 
     timer.start(); 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     ourSong.release(); 
     finish(); 
    } 
} 

回答

1

你需要做些什麼才能順利進行遊戲?更多的CPU,特別是在UI線程中。 (好吧,開個玩笑吧:-)

您可以使用SoundPool並在播放之前預加載歌曲。 SoundPool特別適用於遊戲或類似內容中的短音。

這是加載播放音樂的最短代碼。請記住在AsyncTask中運行它。它可能需要半秒鐘才能開始,但運行起來沒有問題。

SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); 
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
    @Override 
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
     if(status == 0) soundPool.play(sampleId, 1f, 1f, Integer.MAX_VALUE, 0, 1f); 
    } 
}); 
try { 
    soundId = soundPool.load(getAssets().openFd(file), 1); 
} catch (IOException e) { 
    Log.e("TAG", e.getMessage(), e); 
    return; 
} 

它至少需要API等級8,並將音量用於音樂。其他

兩件事情我在你的代碼中看到:

  • 沒有,如果它的Soundpool或媒體播放器的問題。你應該在後臺運行聲音播放部分。
  • 你是怎麼做到的startActivity在另一個線程?這部分不應該工作。
+0

其飛濺的主要活動,應用程序啓動時它dispalyed第一 –

+0

請你適應我的聲音池代碼,即時通訊新本 –

+0

我已經發布了一個新的答案和代碼。希望它有效。 – jboi

0

這裏是我將如何建立一個啓動屏幕上的一些聲音。

它變得相當長,所以讓我對它做一些評論。

  • 因爲它這麼久,我已經把它變成一個新的答案
  • 加載和播放聲音仍然是異步的,有機會的話,那你的閃屏只會閃光燈和未來的活動將立即開始。在這種情況下,您必須使用Java-Lock,如您設置的Semaphoreaquire中的doInBackground以及您在onLoadComplete()末尾的release()

就是這樣。沒有大量測試,但希望你可以使用它的出發點:

public class Splash extends Activity { 
@Override 
protected void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    setContentView(R.layout.splash); 
    /* 
    * I would finish here, to give 
    * Android some time for layout 
    * and other things 
    */ 
} 

/* (non-Javadoc) 
* @see android.app.Activity#onResume() 
*/ 
@Override 
protected void onResume() { 
    super.onResume(); 

    /* 
    * Now start background task 
    * There was a discussion about 
    * Splash screen some time ago. 
    * Especially when you should do the 
    * startActivity and what flags you 
    * should use in the Intent. 
    */ 

    // Start playing sound asynchronously 
    // R.raw.onkar 
    new AsyncTask<Void, Void, Void>() { 
     private SoundPool soundPool = new SoundPool(
       1, AudioManager.STREAM_MUSIC, 0); 

     @Override 
     protected Void doInBackground(Void... params) { 
      // Put in here the code I've already posted! 
      return null; 
     } 

     /* (non-Javadoc) 
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object) 
     */ 
     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      soundPool.release(); 

      Intent openStartingPoint = new Intent(
        "com.sport.sport.MAINLAUNCHER2"); 
      startActivity(openStartingPoint); 
      /* 
      * Actually, you can start the next activity here 
      * or from onResume method with postDelayed(...) 
      * This way here ensures that the Activity is 
      * started right after the sound was played. 
      */ 
     } 
    }.execute((Void) null); 
} 
}