首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

播发音乐时的状态条使用

2012-09-06 
播放音乐时的状态条使用ProgressBar android:id@+id/progresostyle?android:attr/progressBarStyleH

播放音乐时的状态条使用

<ProgressBar android:id="@+id/progreso"        style="?android:attr/progressBarStyleHorizontal"

?

public class Player extends Activity implements Runnable, OnClickListener{   private TextView Status;   private ProgressBar progressBar;   private Button StartMedia;   private Button Stop;   private MediaPlayer mp;         @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);       Status = (TextView) findViewById(R.id.Status);        progressBar = (ProgressBar) findViewById(R.id.progressBar);        StartMedia = (Button) findViewById(R.id.StartMedia);        Stop = (Button) findViewById(R.id.Stop);        StartMedia.setOnClickListener(this);        Stop.setOnClickListener(this);                    }        @Overridepublic void onClick(View v) {        if(v.equals(StartMedia)){            if(mp != null && mp.isPlaying()) return;            mp = MediaPlayer.create(Player.this, R.raw.exodus_piranha);            mp.start();                           Status.setText(R.string.PlayingMedia);                     progressBar.setVisibility(ProgressBar.VISIBLE);            progressBar.setProgress(0);            progressBar.setMax(mp.getDuration());            new Thread(this).start();        }        if(v.equals(Stop) && mp!=null){            mp.stop();            mp = null;                        Status.setText(R.string.Stopped);            progressBar.setVisibility(ProgressBar.GONE);        }}    @Override    public void run() {        int CurrentPosition= 0;        int total = mp.getDuration();        while(mp!=null && CurrentPosition<total){            try {                Thread.sleep(1000);                CurrentPosition= mp.getCurrentPosition();            } catch (InterruptedException e) {                return;            } catch (Exception e){                return;            }                        progressBar.setProgress(CurrentPosition);        }    }}

?

热点排行