2013-10-02 4 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 스레드. (ok, 농담 :-)

SoundPool을 사용하여 재생하기 전에 노래를 미리로드 할 수 있습니다. SoundPool은 특히 게임 내에서 짧은 소리를 내기 위해 만들어졌습니다.

다음은 음악을 재생하는 데 필요한 가장 짧은 코드입니다. AsyncTask에서 실행해야합니다. 시작하는 데 0.5 초가 걸리지 만 문제없이 실행됩니다.

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

의 응용 프로그램이 시작하는 시작의 주요 활동, 최초의 –

+0

하면, 소리 수영장 내 코드에 적응이 –

+0

I에 새로운 메신저하시기 바랍니다 수 dispalyed 코드에 대한 새로운 답변을 올렸습니다. 희망이 작동합니다. – jboi

0

여기에 몇 가지 사운드가 포함 된 스플래시 화면을 만드는 방법은 다음과 같습니다.

아주 오래되었으므로 그것에 대해 언급 해 드리겠습니다.

  • 이 오래 그래서 나는 새 응답 로딩 중
  • 에 넣어 한 소리를 재생하는 것은 당신의 스플래쉬 화면이 바로 플래시 및 다음 활동을 즉시 시작됩니다 것을, 기회가있어, 여전히 비동기 때문에 . 이 경우 과 aquiredoInBackground, release()onLoadComplete()으로 설정하여 Java-Lock을 사용해야합니다.

그게 전부입니다. 많이 그것을 테스트하지만 시작점을 사용할 수 있기를 바랍니다되지 않은 :

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); 
} 
}