当前位置: 代码迷 >> 综合 >> Android 播放器中容易用到的进度条(音量控制,播放进度)
  详细解决方案

Android 播放器中容易用到的进度条(音量控制,播放进度)

热度:77   发布时间:2024-01-05 14:57:37.0
播放器中的的两个常用的进度条,1,显示当前播
播放器中的的两个常用的进度条,1,显示当前播放进度2,显示当前音量package rw.test;
import java.io.IOException;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;@Overridepublic 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{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (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 blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}            }//播放进度条class ProcessBarListener implements OnSeekBarChangeListener{@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stubif (fromUser==true) {mediaPlayer.seekTo(progress);nowPlayTime.setText(ShowTime(progress));}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}}//音量进度条class SeekBarListener implements OnSeekBarChangeListener{@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stubif (fromUser) {int SeekPosition=seekBar.getProgress();audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, SeekPosition, 0);}volumeView.setText(String.valueOf(progress));}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}}//时间显示函数,我们获得音乐信息的是以毫秒为单位的,把把转换成我们熟悉的00:00格式public String ShowTime(int time){time/=1000;int minute=time/60;int hour=minute/60;int second=time%60;minute%=60;return String.format("%02d:%02d", minute, second);}Handler handler=new Handler();public void StrartbarUpdate(){handler.post(r);}Runnable r=new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubint CurrentPosition=mediaPlayer.getCurrentPosition();nowPlayTime.setText(ShowTime(CurrentPosition));int mMax=mediaPlayer.getDuration();ProceseekBar2.setMax(mMax);ProceseekBar2.setProgress(CurrentPosition);handler.postDelayed(r, 100);}};}
 

//把一个名字为test.mp3的文件上传到系统中去,你们懂的.....

00:37 当前播放时间

04:16音乐总时间

11 当前音量

15 总音量

//如有疑问,本人QQ:812200157














  相关解决方案