简体   繁体   English

音乐播放器的进度栏不起作用并崩溃

[英]progress bar for music player doesnt work and crashes

this is my code, i am almost certain the problem is with the 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();

    }

whether or not i have the sleep intervals my result is the same; 我是否有睡眠间隔,我的结果是相同的; the sound starts playing but i cant stop it, and the progress bar doesn't move. 声音开始播放,但我无法停止,进度条也没有移动。 i would appreciate some help 我会感谢一些帮助

I used this code in my app for using seekBar with MediaPlayer. 我在应用程序中使用了此代码,以将seekBar与MediaPlayer结合使用。 It implements Runnable to keep the seekBar updating as the music plays. 它实现了Runnable以在音乐播放时保持seekBar不断更新。 Take a look at the code: 看一下代码:

 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

 }
}

Thanks to: Sridhar Kulkarni 感谢: Sridhar Kulkarni

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM