0

I have been working in an app where i have to play a media with seekbar. The layout consumes recyclerview. The code i used is

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);
  }
}

And after initialising the class like above in my adapter class, in my play button onclick i coded like below.

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();
    }

  }
});

where vh2 is nothing but the ViewHolder2 class instance. I got my song playing on button click, but seek bar not working.

My seekbar initialisation in xml file

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

Can you guys please help on what i am missing in this code?

petey
  • 16,914
  • 6
  • 65
  • 97
Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
  • Are you getting any exceptions? Change `e.printStackTrace();` to `Log.e("player", e.getMessage(), e);` and [check logcat](https://developer.android.com/studio/debug/am-logcat.html) – petey Dec 20 '17 at 17:29
  • @petey E/MediaPlayer: Should have subtitle controller already set... This is the error i am getting in logcat – Kanagalingam Dec 20 '17 at 17:46
  • 1
    Does this help: https://stackoverflow.com/questions/39606413/android-force-close-should-have-subtitle-controller-already-set ? – joao86 Dec 20 '17 at 17:52
  • @joao86 Nope it doesn't help in my case – Kanagalingam Dec 20 '17 at 17:56
  • @RamJay I believe joao86 is right on here. Also, can you add the complete logcat exception stacktrace to the question? This will make your question more helpful for user's searching the same issue in the future (and then upvotes for you) – petey Dec 20 '17 at 18:00

0 Answers0