1,显示当前播放进度
2,显示当前音量
package rw.test;
import Android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class TestMusicActivity extends Activity {
/** Called when the activity is first created. */
private SeekBar SoundseekBar,ProceseekBar2;
private Button button;
private MediaPlayer mediaPlayer;
private TextView nowPlayTime,allTime,volumeView,maxVolumeTextView;
private AudioManager audioManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProceseekBar2=(SeekBar)findViewById(R.id.seekBar1);
SoundseekBar=(SeekBar)findViewById(R.id.seekBar2);
button=(Button)findViewById(R.id.button1);
nowPlayTime=(TextView)findViewById(R.id.textView1);
allTime=(TextView)findViewById(R.id.textView2);
volumeView=(TextView)findViewById(R.id.textView3);
maxVolumeTextView=(TextView)findViewById(R.id.textView4);
button.setOnClickListener(new ButtonListener());
mediaPlayer=new MediaPlayer();
audioManager=(AudioManager)getSystemService(AUDIO_SERVICE);//获取音量服务
int MaxSound=audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);//获取系统音量最大值
maxVolumeTextView.setText(String.valueOf(MaxSound));
SoundseekBar.setMax(MaxSound);//音量控制Bar的最大值设置为系统音量最大值
int currentSount=audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);//获取当前音量
SoundseekBar.setProgress(currentSount);//音量控制Bar的当前值设置为系统音量当前值
SoundseekBar.setOnSeekBarChangeListener(new SeekBarListener());
ProceseekBar2.setOnSeekBarChangeListener(new ProcessBarListener());
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
else {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource("/sdcard/test.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
StrartbarUpdate();
int Alltime= mediaPlayer.getDuration();
allTime.setText(ShowTime(Alltime));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}