音乐播放器的进度条不起作用和崩溃

时间:2016-09-27 19:54:50

标签: java android while-loop

这是我的代码,我几乎可以肯定问题在于while循环。

       public void start(View view) {

        int currentPosition = 0;


        player = MediaPlayer.create(this, R.raw.sound);

        player.start();

        int total = player.getDuration();
        progressBar.setProgress(0);
        progressBar.setMax(player.getDuration());

        while (player != null && currentPosition < total) {
//            try {
//                Thread.sleep(500);

                currentPosition = player.getCurrentPosition();

//            } catch (InterruptedException e) {
//                return;
//            } catch (Exception e) {
//                return;
//            }

            progressBar.setProgress(currentPosition);
        }
    }


    public void stop(View view) {

        player.stop();

    }

我是否有睡眠间隔我的结果是一样的;声音开始播放,但我无法阻止它,进度条不会移动。 我希望得到一些帮助

1 个答案:

答案 0 :(得分:0)

我在我的应用程序中使用此代码将seekBar与MediaPlayer一起使用。它实现 Runnable 以在播放音乐时保持seekBar更新。看看代码:

 public class MainActivity extends Activity implements Runnable,
  OnClickListener, OnSeekBarChangeListener {
 private SeekBar seekBar;
 private Button startMedia;
 private Button stopMedia;
 private MediaPlayer mp;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  seekBar = (SeekBar) findViewById(R.id.seekBar1);
  startMedia = (Button) findViewById(R.id.button1);
  stopMedia = (Button) findViewById(R.id.button2);
  startMedia.setOnClickListener(this);
  stopMedia.setOnClickListener(this);
  seekBar.setOnSeekBarChangeListener(this);
  seekBar.setEnabled(false);
 }

 public void run() {
  int currentPosition = mp.getCurrentPosition();
  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 = MediaPlayer.create(getApplicationContext(), R.raw.song2);
    seekBar.setEnabled(true);
   }
   if (mp.isPlaying()) {
    mp.pause();
    startMedia.setText("play");
   } else {
    mp.start();
    startMedia.setText("pause");
    seekBar.setMax(mp.getDuration());
    new Thread(this).start();
   }
  }

  if (v.equals(stopMedia) && mp != null) {
   if (mp.isPlaying() || mp.getDuration() > 0) {
    mp.stop();
    mp = null;
    startMedia.setText("play");
    seekBar.setProgress(0);
   }
  }

 }

 public void onProgressChanged(SeekBar seekBar, int progress,
   boolean fromUser) {
  try {
   if (mp.isPlaying() || mp != null) {
    if (fromUser)
     mp.seekTo(progress);
   } else if (mp == 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

 }
}

感谢:Sridhar Kulkarni