2016-07-14 1 views
2

android studio에서 mediaplayer 클래스로 작업 중입니다. 나는 단순히 하나의 사운드를 페이드 아웃시키고 setVolume (0,0)과 setVolume (1,1)을 사용하는 대신에 다른 사운드로 페이드하고 싶을뿐입니다.Android Studio Mediaplayer 페이드 인 및 아웃 방법

나는이 두 미디어 플레이어를 만들었으며이 스레드에서 해결책을 찾은 것처럼 보였습니다. Android: How to create fade-in/fade-out sound effects for any music file that my app plays? 그러나 deltaTime을 사용하는 방법을 모르겠습니다.

나는 거의 이해할 수없는 다른 해결책이 있습니다. 미디어 플레이어 두 대를 쉽게 사용할 수있는 방법이 없습니까? 아무도 이것을 필요로하지 않았거나 모든 사람들이 강박적인 코드를 사용하여 그것을 달성했다고 상상할 수 없습니다. 델타 시간은 어떻게 사용해야합니까?

답변

2

링크 된 example을 보면 일정 기간 동안 볼륨을 높이거나 낮추려면 루프에서 fadeIn()/fadeOut()을 호출해야합니다. deltaTime은 루프의 각 반복 사이의 시간이됩니다.

메인 UI 스레드와 별도의 스레드에서이 작업을 수행해야하므로이를 차단하지 않아 앱이 중단 될 수 있습니다. 새로운 Thread/Runnable/Timer에이 루프를 넣음으로써이를 수행 할 수 있습니다. 나는 귀하의 경우 사용 하나에서와 트랙을 교체,

int volume = 0; 

private void startFadeIn(){ 
    final int FADE_DURATION = 3000; //The duration of the fade 
    //The amount of time between volume changes. The smaller this is, the smoother the fade 
    final int FADE_INTERVAL = 250; 
    final int MAX_VOLUME = 1; //The volume will increase from 0 to 1 
    int numberOfSteps = FADE_DURATION/FADE_INTERVAL; //Calculate the number of fade steps 
    //Calculate by how much the volume changes each step 
    final float deltaVolume = MAX_VOLUME/(float)numberOfSteps; 

    //Create a new Timer and Timer task to run the fading outside the main UI thread 
    final Timer timer = new Timer(true); 
    TimerTask timerTask = new TimerTask() { 
     @Override 
     public void run() { 
      fadeInStep(deltaVolume); //Do a fade step 
      //Cancel and Purge the Timer if the desired volume has been reached 
      if(volume>=1f){ 
       timer.cancel(); 
       timer.purge(); 
      } 
     } 
    }; 

    timer.schedule(timerTask,FADE_INTERVAL,FADE_INTERVAL); 
} 

private void fadeInStep(float deltaVolume){ 
    mediaPlayer.setVolume(volume, volume); 
    volume += deltaVolume; 

} 

대신 두 개의 MediaPlayer를 개체를 사용하여 : 여기

내 (당신이 페이드 아웃에 대한 비슷한 일을 할 수있는) 페이딩에 대한 예입니다 페이드 사이. 예 :

**Audio track #1 is playing but coming to the end** 
startFadeOut(); 
mediaPlayer.stop(); 
mediaPlayer.reset(); 
mediaPlayer.setDataSource(context,audiofileUri); 
mediaPlayer.prepare(); 
mediaPlayer.start(); 
startFadeIn(); 
**Audio track #2 has faded in and is now playing** 

희망이 당신의 문제를 해결합니다.

+0

아직 다른 해결책을 찾지 못해서 도움이됩니다. 또한 새로운 스레드에서 이것을 실행하는 힌트도 매우 유용합니다! 감사! – olop01

관련 문제