Seekbar没有设置Mediaplayer android

时间:2017-12-20 17:15:57

标签: java android android-mediaplayer android-seekbar

我一直在应用程序中工作,我必须使用搜索栏播放媒体。布局消耗了recyclerview。我使用的代码是

private class ViewHolder2 extends RecyclerView.ViewHolder {
  public TextView name, filename;
  ImageView img, play;
  SeekBar seekBar;

  public ViewHolder2(View view) {
    super(view);
    name = (TextView) view.findViewById(R.id.name);
    filename = (TextView) view.findViewById(R.id.filename);
    img = (ImageView) view.findViewById(R.id.image);
    play = (ImageView) view.findViewById(R.id.play);
    seekBar = (SeekBar) view.findViewById(R.id.seekbar);
  }
}

在我的适配器类中初始化类之后,在我的播放按钮onclick中我编码如下。

vh2.play.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {

    if (isFilePresent("Bombay.mp3")) {
      final MediaPlayer mediaPlayer1 = new MediaPlayer();
      try {
        if (mediaPlayer1 != null) {
          mediaPlayer1.setDataSource(audioPath);
          mediaPlayer1.prepare();
          mediaPlayer1.start();

        }

        int mediaPlayerDuration = mediaPlayer1.getDuration();
        vh2.seekBar.setMax(mediaPlayerDuration);

        vh2.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
          public void onStopTrackingTouch(SeekBar seekBar) {

          }

        @Override
          public void onStartTrackingTouch(SeekBar seekBar) {

          }

        @Override
          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (mediaPlayer1 != null && fromUser) {
              mediaPlayer1.seekTo(progress * 1000);
            }
          }
        });

        handler = new Handler();
//Make sure you update Seekbar on UI thread
        ((Activity) context).runOnUiThread(new Runnable() {

        @Override
          public void run() {
            if (mediaPlayer1 != null) {
              int mCurrentPosition = mediaPlayer1.getCurrentPosition() / 1000;
              vh2.seekBar.setProgress(mCurrentPosition);
            }
            handler.postDelayed(this, 1000);
          }
        });

      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      Toast.makeText(context, "Oops! File not found!", Toast.LENGTH_SHORT).show();
    }

  }
});

其中vh2只是ViewHolder2类实例。我按下按钮点击了我的歌曲,但是找不到工作。

我在xml文件中的seekbar初始化

<SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

你们可以帮忙解释一下我在这段代码中缺少的东西吗?

0 个答案:

没有答案