获取时间经过MP3文件流的android和持续时间

时间:2013-07-01 08:53:41

标签: android android-mediaplayer

当我的媒体播放器开始播放时,我希望从播放器开始到播放媒体结束时间过去。例如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

    }

}

2 个答案:

答案 0 :(得分:2)

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

// returns current position
mediaPlayer.getCurrentPosition();

//returns total duration
mediaPlayer.getDuration(); 

希望这会有所帮助

答案 1 :(得分:1)

启动媒体播放器时实施runnable:

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();