六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 50|回复: 0

播放音乐ProgressBar进度条

[复制链接]

升级  82%

292

主题

292

主题

292

主题

进士

Rank: 4

积分
910
 楼主| 发表于 2013-1-15 02:28:34 | 显示全部楼层 |阅读模式
<ProgressBar android:id="@+id/progreso" 
        style="?android:attr/progressBarStyleHorizontal" 
import android.app.Activity
import android.media.MediaPlayer
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
 
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);                 
    }         
 
@Override 
public 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); 
        } 
    } 
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表