这篇,我会将如何实现进度控制和音量控制的代码进行讲解。
一、进度控制:
布局文件:
<SeekBar android:id="@+id/seekBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/textViewCurTime" android:layout_toLeftOf="@id/textViewTotalTime" android:paddingLeft="5dp" android:paddingRight="5dp" android:max="100" android:progressDrawable="@drawable/seekbar_style" android:thumb="@drawable/seekbar_thumb" android:background="@drawable/progress_buffering" > </SeekBar>
java代码:
获得View并为它设置监听器
mPlayProgress = (SeekBar) findViewById(R.id.seekBar); mPlayProgress.setOnSeekBarChangeListener(new SeekBarChangeListener());
private class SeekBarChangeListener implements OnSeekBarChangeListener { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { switch(seekBar.getId()) { case R.id.seekBar: if (fromUser) { mServiceManager.seekTo(progress); // 用户控制进度的改变 } break; case R.id.GL_player_voice: // 设置音量 am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0); System.out.println("am--->" + progress); break; } }
下面是seekTo方法:
public boolean seekTo(int rate) { if (mMusicConnect != null) { try { return mMusicConnect.seekTo(rate); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return false; }
这样,一个进度条就完成了。
二、音量控制
布局文件:
<RelativeLayout android:visibility="gone" android:layout_toLeftOf="@id/buttonVolumn" android:layout_toRightOf="@id/buttonMode" android:id="@+id/ll_player_voice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/player_progresslayout_bg" > <SeekBar android:id="@+id/GL_player_voice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:paddingLeft="5dp" android:paddingRight="5dp" android:progressDrawable="@drawable/seekbar_style" android:max="15" android:background="@drawable/progress_buffering" android:thumb="@drawable/voice_seekbar_thumb" /> </RelativeLayout>
里面那个android:visibility="gone"就是隐藏的意思,如果你对visibility不熟悉,可以参照我的博文http://blog.csdn.net/google_acmer/article/details/19342643
那里有我详尽的解释。
java代码:
首先是声明,并获得音频服务对象
//音量调节面板显示和隐藏的动画
showVoicePanelAnimation = AnimationUtils.loadAnimation(MusicPlayActivity.this, R.anim.push_up_in);
hiddenVoicePanelAnimation = AnimationUtils.loadAnimation(MusicPlayActivity.this, R.anim.push_up_out);
//获得系统音频管理服务对象
am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
GL_player_voice.setProgress(currentVolume);
接下来是隐藏面板的控制以及音量控制
//控制显示音量控制面板的动画
public void voicePanelAnimation() {
if(ll_player_voice.getVisibility() == View.GONE) {
ll_player_voice.startAnimation(showVoicePanelAnimation);
ll_player_voice.setVisibility(View.VISIBLE);
}
else{
ll_player_voice.startAnimation(hiddenVoicePanelAnimation);
ll_player_voice.setVisibility(View.GONE);
}
}
private class SeekBarChangeListener implements OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
switch(seekBar.getId()) {
case R.id.seekBar:
if (fromUser) {
mServiceManager.seekTo(progress); // 用户控制进度的改变
}
break;
case R.id.GL_player_voice:
// 设置音量
am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
System.out.println("am--->" + progress);
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}