(Android)如何在我的音乐播放器上显示时间

时间:2015-07-15 04:54:36

标签: java android android-mediaplayer

音乐可以正常播放但我无法在我的音乐播放器上显示音乐时间。我该怎么办?

在主要班级

 public void onClick(View v) {

     if (mMedia.isPlaying()) {
            txtView.setText("Playing : music.mp3....");
                mMedia.pause();
            } else {
                txtView.setText("pause : music.mp3....");
                mMedia.start();
            }
        }
private void UpdateseekChange(View v){
    if(mMedia.isPlaying()){
        SeekBar sb = (SeekBar)v;
        mMedia.seekTo(sb.getProgress());
    }

3 个答案:

答案 0 :(得分:1)

试试这段代码:)

//Get duration
long totalDuration = mediaPlayerOBJ.getDuration();
//Get current time
long currentDuration = mediaPlayerOBJ.getCurrentPosition();

textViewCurrentTime.setText(milliSecondsToTimer(currentDuration));
textViewDuration.setText(milliSecondsToTimer(totalDuration));

//Here is function to convert milliseconds to timer

public String milliSecondsToTimer(long milliseconds) {
String finalTimerString = "";
String secondsString = "";

// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
 if (hours > 0) {
    finalTimerString = hours + ":";
}

// Prepending 0 to seconds if it is one digit
  if (seconds < 10) {
    secondsString = "0" + seconds;
}   else {
    secondsString = "" + seconds;
}

finalTimerString = finalTimerString + minutes + ":" + secondsString;

// return timer string
return finalTimerString;
}

答案 1 :(得分:0)

It's the all code page of the application media player.How can I do?

在主要班级

private MediaPlayer mMedia;
private Handler handler = new Handler();
private SeekBar seekBar;

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

    if(mMedia != null){
        mMedia.release();
    }

    final TextView txtView = (TextView)findViewById(R.id.textView1);
    txtView.setText("Source : music.mp3");

    /* Resource in R.
     * mMedia = MediaPlayer.create(this, R.raw.music);
     * mMedia.start();
     */

    /*
     * from DataSource
     * mMedia = new MediaPlayer();
     * mMedia.setDataSource("http://www.thaicreate.com/music/mymusic.mp3");
     * mMedia.start();
     *
     */
    mMedia = MediaPlayer.create(this, R.raw.music);

    seekBar = (SeekBar)findViewById(R.id.seekBar1);
    seekBar.setMax(mMedia.getDuration());
    seekBar.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            UpdateseekChange(v);

            return false;
        }
    });


    final Button btn1 = (Button) findViewById(R.id.button1); // Start



    // Start
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (mMedia.isPlaying()) {
            txtView.setText("Playing : music.mp3....");
                mMedia.pause();
            } else {
                txtView.setText("pause : music.mp3....");
                mMedia.start();
            }
        }
    });


}

private void UpdateseekChange(View v){
    if(mMedia.isPlaying()){
        SeekBar sb = (SeekBar)v;
        mMedia.seekTo(sb.getProgress());
    }
}

public void startPlayProgressUpdater() {
    seekBar.setProgress(mMedia.getCurrentPosition());

    if (mMedia.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }
}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if(mMedia != null && fromUser){
        mMedia.seekTo(progress * 1000);
    }
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if(mMedia != null){
        mMedia.release();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
 }

}

答案 2 :(得分:0)

好的,如果你想用String显示,就用这个方法。 它获取剩余长度(持续时间)并返回字符串:

public String convertTime(long value) {
    String audioTime;
    int dur = (int) value;
    int hrs = (dur / 3600000);
    int mns = (dur / 60000) % 60000;
    int scs = dur % 60000 / 1000;

    if (hrs > 0) {
        audioTime = String.format("%02d:%02d:%02d", hrs, mns, scs);
    } else {
        audioTime = String.format("%02d:%02d", mns, scs);
    }
    return audioTime;
}