播放音量渐变

这个不是什么比较难的事情,在handler中做了个delayed的循环,效果还不错,细节方面要注意,这里小计一下

 

private Handler mMediaplayerHandler = new Handler() { float mCurrentVolume = 1.0f; @Override public void handleMessage(Message msg) { switch (msg.what) { case FADEIN: if (!isPlaying()) { mCurrentVolume = 0f; setVolume(mCurrentVolume); play(); mMediaplayerHandler.sendEmptyMessageDelayed(FADEIN, 10); } else { mCurrentVolume += 0.01f; if (mCurrentVolume < 1.0f) { mMediaplayerHandler.sendEmptyMessageDelayed(FADEIN, 10); } else { mCurrentVolume = 1.0f; } setVolume(mCurrentVolume); } break; default: break; } } }; private void setVolume(float currentVolume){ mPlayer.setVolume(currentVolume, currentVolume); } private boolean isPlaying() { if(mPlayer == null) { return false; } return mPlayer.isPlaying(); } private void play(){ if(mPlayer == null) { return; } mPlayer.start(); } public void onDestroy() { if(mPlayer != null) { mPlayer.release(); } super.onDestroy(); } 

你可能感兴趣的:(null,float)