如何在播放时从0.00秒移动当前时间位置

时间:2017-03-25 06:31:10

标签: android android-mediaplayer

这是java文件:

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class AudioPlayer extends AppCompatActivity {
    ImageView mIvPlayControl;
    TextView mTvTotalDuration;
    TextView mTvCurrentPosition;
    MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_player2);

        mIvPlayControl= (ImageView) findViewById(R.id.iv_play);
        mTvTotalDuration= (TextView) findViewById(R.id.tv_total);
        mTvCurrentPosition= (TextView) findViewById(R.id.tv_progress);
        mp = MediaPlayer.create(AudioPlayer.this, R.raw.sleep_away);
        int mCurrentPosition=mp.getCurrentPosition();
        int duration=mp.getDuration(); // miliseconds time
        //convert into seconds
        duration=duration/1000;
        mCurrentPosition=mp.getCurrentPosition()/1000;
        Log.e("value","value: "+duration);
        mTvTotalDuration.setText(String.valueOf(duration));
        mTvCurrentPosition.setText(String.valueOf(mCurrentPosition));

        mIvPlayControl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp.isPlaying()) {
                    mp.pause();
                    mIvPlayControl.setImageResource(R.mipmap.play_small);

                } else {
                    mp.start();
                    mIvPlayControl.setImageResource(R.mipmap.pause_small);
                }

            }
        });

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3"
        android:gravity="center"
        >
        <TextView
            android:id="@+id/tv_progress"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="0.00"
            />

        <ImageView
            android:id="@+id/iv_play"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/play_small"

            />

        <TextView
            android:id="@+id/tv_total"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:text="0.00"
            />
    </LinearLayout>

</LinearLayout>

如何在播放时将开始时间0及以后移动?我是否必须添加搜索条才能获得移动时间?有人可以帮忙吗?可以播放歌曲,但计时器不起作用。更多细节或问题在评论中。

1 个答案:

答案 0 :(得分:0)

假设mTvCurrentPlayTime是表示屏幕上当前播放时间的TesxtView,那么您可以使用以下代码更新视图:

Handler mHandler = new Handler();
//Make sure you update TextView on UI thread
AudioPlayer.this.runOnUiThread(new Runnable() {

    @Override
    public void run() {
        if(mp!= null){
            int mCurrentPosition = mp.getCurrentPosition() / 1000;
            mTvCurrentPosition.setText(String.valueOf(mCurrentPosition));
        }
        mHandler.postDelayed(this, 1000);
    }
});