Android SeekBar进度不起作用?

时间:2016-03-23 15:41:12

标签: android multithreading handler seekbar

我正在尝试向从互联网流式传输的媒体播放器添加搜索栏。但是,由于某种原因,它无法正常工作。我正在使用处理程序每​​秒更新UI。 这是我底部的代码:

public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
boolean isPrepared = false;
ImageButton imageButton;
Handler handler;
SeekBar seekBar;
TextView notifying;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    imageButton = (ImageButton) findViewById(R.id.imageButton);
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    notifying = (TextView) findViewById(R.id.notifying);

    handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {

            if (mediaPlayer != null) {
                int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
                Log.i("message", mCurrentPosition + "");
                seekBar.setProgress(mCurrentPosition);

            }
            handler.postDelayed(this, 1000);
        }
    };
    handler.postDelayed(runnable, 1000);


    try {
        mediaPlayer.setDataSource("Any mp3 souund from the internet");


    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.i("message", "prepared");
            isPrepared = true;
        }
    });
    mediaPlayer.prepareAsync();
    seekBar.setMax(mediaPlayer.getDuration());
}

public void play(View view) {
    if (!mediaPlayer.isPlaying() && isPrepared == true) {
        Log.i("message", isPrepared + "");
        Log.i("message", mediaPlayer.isPlaying() + "");
        Log.i("message", (mediaPlayer.getDuration() / 1000) / 60 + "");
        mediaPlayer.start();
        imageButton.setImageResource(R.drawable.pause);

    } else {

        mediaPlayer.pause();
        imageButton.setImageResource(R.drawable.play);
    }
}

public void stop(View view) {
    if (mediaPlayer != null & mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
        mediaPlayer.seekTo(0);

        imageButton.setImageResource(R.drawable.play);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mediaPlayer != null) {
        mediaPlayer.release();
        mediaPlayer = null;

    }
}

1 个答案:

答案 0 :(得分:1)

我不确定但是试试这个:

将此行放在preperalistener回调中

    seekBar.setMax(mediaPlayer.getDuration());
像这样:

 mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.i("message", "prepared");
            isPrepared = true;
            seekBar.setMax(mediaPlayer.getDuration());

        }
    });

另外我相信int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;你不应该除以1000。

其他seekBar.setMax(mediaPlayer.getDuration()/1000);