0

当我的媒体播放器开始播放时,我想获取从播放器开始到正在播放的媒体结束所经过的时间。例如 00:01, 00:02, 00:03, 00:04... 我怎样才能做到这一点?下面是我的媒体播放器类。

public class EntityPageActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
    private SeekBar seekBar;
    private Button startMedia;
    private Button pauseMedia;
    private MediaPlayer mp;
    Uri url;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample);

        AudioControl();    
    }

    public void AudioControl(){

        seekBar = (SeekBar) findViewById(R.id.seekBar);
        startMedia = (Button) findViewById(R.id.play);
        pauseMedia = (Button) findViewById(R.id.pause);
        startMedia.setOnClickListener(this);
        pauseMedia.setOnClickListener(this); 

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
             // TODO Auto-generated method stub
                //System.out.println("maxvolume"+Integer.toString(maxVolume));
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
             // TODO Auto-generated method stub
            }

            @Override
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
             // TODO Auto-generated method stub
                    mp.seekTo(arg1); 
                    seekBar.setProgress(arg1);

            }

       });  






    }

    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;
            }            
            seekBar.setProgress(currentPosition);
        }
    }

    public void onClick(View v) {
        if (v.equals(startMedia)) {
            if (mp != null && mp.isPlaying()) return;
            if(seekBar.getProgress() > 0) {
                mp.start();
                return;
            }

            mp = MediaPlayer.create(EntityPageActivity.this,Uri.parse("http://cdn-preview-e.deezer.com/stream/ed403858b3532e9c07c99d7015269848-2.mp3"));

            mp.start();                     
            seekBar.setProgress(0);
            seekBar.setMax(mp.getDuration());

            new Thread(this).start();

        }

        if (v.equals(pauseMedia) && mp!=null) {
            mp.pause();
        }       

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

}
4

2 回答 2

2

这是这两种方法,一种是获取媒体播放器当前位置,第二种是获取总持续时间

// returns current position
mediaPlayer.getCurrentPosition();

//returns total duration
mediaPlayer.getDuration(); 

希望这会有所帮助

于 2013-07-01T09:33:21.473 回答
1

启动媒体播放器时实现一个可运行文件:

mp.start();
Handler handler = new Handler();
Runnable runnable = new Runnable() {
                        public void run() {
                            while (mp.isPlaying()) {
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                handler.post(new Runnable() {
                                    public void run() {
                                        seekbar.setMax(mp.getDuration() / 1000);
                                        currTime.setText(getDurationString(mp
                                                .getCurrentPosition() / 1000));
                                        seekbar.setProgress(mp
                                                .getCurrentPosition() / 1000);
                                    }
                                });
                            }
                        }
                    };
                    new Thread(runnable).start();
于 2013-12-26T09:14:22.493 回答