搜索栏不在媒体播放器中滚动

时间:2016-03-08 15:26:44

标签: android android-mediaplayer android-seekbar

我创建了一个简单的Android媒体播放器,它工作得很好,只是搜索栏没有移动。当我手动移动搜索栏时,它工作正常并且歌曲它前进和后退。但它并没有自动移动。以下是代码;

public class Datadetail extends AppCompatActivity implements Runnable, OnSeekBarChangeListener{

TextView txtname;
TextView txtaarti;
String[] itemname;
int position;
int resID;
private SeekBar seekbar;
ImageButton buttonPlay;
private MediaPlayer mplayer;

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

    Intent i = getIntent();

    position = i.getExtras().getInt("position");
    itemname = i.getStringArrayExtra("itemname");

    txtname = (TextView) findViewById(R.id.textView2);
    txtname.setText(itemname[position]);

    txtaarti = (TextView) findViewById(R.id.textView3);
    txtaarti.setText(Aarti.aartitxt[position]);

    seekbar = (SeekBar) findViewById(R.id.seekBar);
    seekbar.setOnSeekBarChangeListener(this);
    seekbar.setEnabled(false);

    buttonPlay = (ImageButton) findViewById(R.id.imageButton);

    playsong(position);


    buttonPlay.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if (mplayer.isPlaying()){
                if (mplayer != null){
                    mplayer.pause();
                    buttonPlay.setImageResource(R.mipmap.ic_play);
                }
            }else{
                if (mplayer != null){
                    mplayer.start();
                    seekbar.setMax(mplayer.getDuration());
                    new Thread(String.valueOf(this)).start();
                    buttonPlay.setImageResource(R.mipmap.ic_pause);

                }
            }
            if (mplayer == null) {
                if (mplayer.isPlaying() || mplayer.getDuration() > 0) {
                    mplayer.stop();
                    mplayer = null;
                    seekbar.setProgress(0);
                }
            }

        }
    });
}

 public void playsong(int position) {
            switch (position){
                case 0:
                    resID = R.raw.ganesh_aarti;
                    break;
                case 1:
                    resID = R.raw.durga_aarti;
                    break;

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

     mplayer = MediaPlayer.create(this, resID);
     seekbar.setEnabled(true);
     buttonPlay.setImageResource(R.mipmap.ic_play);

 }

@Override
public void onBackPressed() {
    if (mplayer.isPlaying()) {
        mplayer.stop();
    }
    mplayer.release();
    super.onBackPressed();
}
public void run() {
    int currentPosition = mplayer.getCurrentPosition();
    int total = mplayer.getDuration();

    while (mplayer != null && currentPosition < total) {
        try {
            Thread.sleep(1000);
            currentPosition = mplayer.getCurrentPosition();
        } catch (InterruptedException e) {
            return;
        } catch (Exception e) {
            return;
        }
        seekbar.setProgress(currentPosition);
    }
}
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
    try {
        if (mplayer.isPlaying() || mplayer != null) {
            if (fromUser)
                mplayer.seekTo(progress);
        } else if (mplayer != null) {
            Toast.makeText(getApplicationContext(), "Media is not running",
                    Toast.LENGTH_SHORT).show();
            seekbar.setProgress(0);
        }
    } catch (Exception e) {
        Log.e("seek bar", "" + e);
        seekbar.setEnabled(false);

    }
}

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

}

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

}

}

1 个答案:

答案 0 :(得分:1)

这是答案,现在工作正常;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.os.Handler;

public class Datadetail1 extends AppCompatActivity implements OnSeekBarChangeListener{

TextView txtname;
TextView txtaarti;
String[] itemname;
int position;
int resID;
private SeekBar seekbar;
ImageButton buttonPlay;
private MediaPlayer mplayer;
private Handler mHandler = new Handler();
private Utilities utils;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;

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

    Intent i = getIntent();

    position = i.getExtras().getInt("position");
    itemname = i.getStringArrayExtra("itemname");

    txtname = (TextView) findViewById(R.id.textView2);
    txtname.setText(itemname[position]);

    txtaarti = (TextView) findViewById(R.id.textView3);
    txtaarti.setText(Aarti.aartitxt[position]);

    seekbar = (SeekBar) findViewById(R.id.seekBar);
    seekbar.setOnSeekBarChangeListener(this);
    seekbar.setEnabled(true);

    songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
    songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
    utils = new Utilities();
    mplayer = new MediaPlayer();

    buttonPlay = (ImageButton) findViewById(R.id.imageButton);

    playsong(position);

    buttonPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mplayer.isPlaying()) {
                if (mplayer != null) {
                    mplayer.pause();
                    buttonPlay.setImageResource(R.mipmap.ic_play);
                }
            } else {
                if (mplayer != null) {
                    mplayer.start();
                    buttonPlay.setImageResource(R.mipmap.ic_pause);

                }
            }

        }
    });
}

public void playsong(int position) {
    switch (position){
        case 0:
            resID = R.raw.ganesh_aarti;
            break;
        case 1:
            resID = R.raw.durga_aarti;
            break;

    }
    if (mplayer !=null){
        mplayer.release();
    }
        mplayer = MediaPlayer.create(this, resID);
        buttonPlay.setImageResource(R.mipmap.ic_play);
        seekbar.setProgress(0);
        seekbar.setMax(100);
        updateProgressBar();

}
public void updateProgressBar() {
    mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        int totalDuration = mplayer.getDuration();
        int currentDuration = mplayer.getCurrentPosition();

        // Displaying Total Duration time
       songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
        // Displaying time completed playing
       songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));

        // Updating progress bar
        int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
        //Log.d("Progress", ""+progress);
        seekbar.setProgress(progress);

        // Running this thread after 100 milliseconds
        mHandler.postDelayed(this, 100);
    }
};


@Override
public void onBackPressed() {
    if (mplayer.isPlaying()) {
        mplayer.stop();
    }
    mHandler.removeCallbacks(mUpdateTimeTask);
    mplayer.release();
    super.onBackPressed();
}

public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {

}

@Override
public void onStartTrackingTouch(SeekBar seekbar) {
    // TODO Auto-generated method stub
    mHandler.removeCallbacks(mUpdateTimeTask);

}

@Override
public void onStopTrackingTouch(SeekBar seekbar) {
    // TODO Auto-generated method stub
    mHandler.removeCallbacks(mUpdateTimeTask);
    int totalDuration = mplayer.getDuration();
    int currentPosition = utils.progressToTimer(seekbar.getProgress(), totalDuration);

    // forward or backward to certain seconds
    mplayer.seekTo(currentPosition);

    // update timer progress again
    updateProgressBar();

}

}