0

Here is the java file:

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

            }
        });

And here is the layout file:

<?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>

How do i move the beginning time 0 and onwards when played? Do i have to add seek bar to get moving time? Can somebody please help here? The song can be played but timer doesn't work. And more details or questions are in the comments.

Shubhang Agarwal
  • 39
  • 1
  • 1
  • 6

1 Answers1

0

Suppose mTvCurrentPlayTime is TesxtView representing current play-time on the screen then you can use following code to update the view:

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);
    }
});
Rohit Gupta
  • 1,368
  • 1
  • 14
  • 30