-2

The issue is with these lines

 seekBar1.setProgress(player.getCurrentPosition());

and

seekBar2.setProgress(player2.getCurrentPosition());

Error

Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object reference

also, a small request I have an issue with the handler.postDelayed can you please check this user question too I have the same issue, here is the link to that question

Code // full code is belove this

public class UpdateSeekBar1 implements Runnable {

        @Override
        public void run() {
            currentTime1.setText(createTimerLable1(seekBar1.getProgress()));
            seekBar1.setProgress(player.getCurrentPosition());
            handler1.postDelayed(this, 100);

        }
    }

    public class UpdateSeekBar2 implements Runnable {

        @Override
        public void run() {
            currentTime2.setText(createTimerLable2(seekBar2.getProgress()));
            seekBar2.setProgress(player2.getCurrentPosition());
            handler2.postDelayed(this, 100);

        }
    }

Full Code // the code can be a little confusing and written badly

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    MediaPlayer player, player2;
    SeekBar seekBar1, seekBar2;
    TextView currentTime1, currentTime2;
    TextView totalTime1, totalTime2;
    Handler handler1 = new Handler();
    Handler handler2 = new Handler();
    ImageView play, pause, stop, play2, pause2, stop2;
    int pauseCurrentPosition, pauseCurrentPosition2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        player = MediaPlayer.create(this, R.raw.dog_howl);
        player2 = MediaPlayer.create(this, R.raw.dog_bark);
        seekBar1 = findViewById(R.id.seekbar1);
        seekBar2 = findViewById(R.id.seekbar2);
        play = findViewById(R.id.playbtn);
        pause = findViewById(R.id.pausebtn);
        stop = findViewById(R.id.stopbtn);
        play2 = findViewById(R.id.playbtn2);
        pause2 = findViewById(R.id.pausebtn2);
        stop2 = findViewById(R.id.stopbtn2);
        currentTime1 = findViewById(R.id.currentTime1);
        currentTime2 = findViewById(R.id.currentTime2);
        totalTime1 = findViewById(R.id.totalTime1);
        totalTime2 = findViewById(R.id.totalTime2);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
        play2.setOnClickListener(this);
        pause2.setOnClickListener(this);
        stop2.setOnClickListener(this);
        seekBar1.setMax(player.getDuration());
        seekBar2.setMax(player2.getDuration());
        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                player.seekTo(progress);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                player2.seekTo(i);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        String totTime1 = createTimerLable1(player.getDuration());
        totalTime1.setText(totTime1);
        String totTime2 = createTimerLable2(player2.getDuration());
        totalTime1.setText(totTime2);

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.playbtn:
                UpdateSeekBar1 updateSeekBar1 = new UpdateSeekBar1();
                handler1.post(updateSeekBar1);
                if (player == null) {
                    player = MediaPlayer.create(getApplicationContext(), R.raw.dog_howl);
                    player.start();


                } else if (!player.isPlaying()) {
                    player.seekTo(pauseCurrentPosition);
                    player.start();


                }
                break;
            case R.id.pausebtn:

                if (player != null) {
                    player.pause();
                    pauseCurrentPosition = player.getCurrentPosition();
                }
                break;
            case R.id.stopbtn:
                if (player != null) {
                    player.stop();
                    player = null;
                }
                break;
            case R.id.playbtn2:
                UpdateSeekBar2 updateSeekBar2 = new UpdateSeekBar2();
                handler2.post(updateSeekBar2);
                if (player2 == null) {
                    player2 = MediaPlayer.create(getApplicationContext(), R.raw.dog_bark);
                    player2.start();
                } else if (!player2.isPlaying()) {
                    player2.seekTo(pauseCurrentPosition2);
                    player2.start();

                }
                break;
            case R.id.pausebtn2:
                if (player2 != null) {
                    player2.pause();
                    pauseCurrentPosition2 = player2.getCurrentPosition();
                }
                break;
            case R.id.stopbtn2:
                if (player2 != null) {
                    player2.stop();
                    player2 = null;
                }
                break;

        }
    }

    public String createTimerLable1(int duration) {
        String timerLabel = "";
        int min = duration / 1000 / 60;
        int sec = duration / 1000 % 60;
        timerLabel += min + ":";
        if (sec < 10) timerLabel += "0";
        timerLabel += sec;
        return timerLabel;


    }

    public String createTimerLable2(int duration) {
        String timerLabel = "";
        int min = duration / 1000 / 60;
        int sec = duration / 1000 % 60;
        timerLabel += min + ":";
        if (sec < 10) timerLabel += "0";
        timerLabel += sec;
        return timerLabel;


    }

    public class UpdateSeekBar1 implements Runnable {

        @Override
        public void run() {
            currentTime1.setText(createTimerLable1(seekBar1.getProgress()));
            seekBar1.setProgress(player.getCurrentPosition());
            handler1.postDelayed(this, 100);

        }
    }

    public class UpdateSeekBar2 implements Runnable {

        @Override
        public void run() {
            currentTime2.setText(createTimerLable2(seekBar2.getProgress()));
            seekBar2.setProgress(player2.getCurrentPosition());
            handler2.postDelayed(this, 100);

        }
    }

}
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Vasant Raval
  • 257
  • 1
  • 12
  • 31
  • why is this not just a duplicate which was provided when your previous question was closed ? is this not just a simple nullpointer ? i suggest only asking one question at a time as well, `can you please check this user question too I have the same issue,` isn't going to work too well – a_local_nobody Feb 04 '22 at 07:03
  • tell me a thing, if there is a question already asked and if I also have the same issue and I mentioned that question in my current question so I don't have to write a duplicate question what's wrong with it (I'm asking genuinely ) or i just ask it again so there will be a new duplicate question – Vasant Raval Feb 04 '22 at 07:09
  • well, it's a difficult situation, i don't think asking an existing question again would be a bad thing if all of the existing questions don't have an answer, especially if the question was asked a long time ago (like 5+ years), i probably wouldn't downvote for that, so perhaps try it, i also didn't downvote here but i do agree that the error you're facing here is a nullpointer and 99% of the time nullpointers are solved the same way. you could also add a bounty to your question to draw more attention to it – a_local_nobody Feb 04 '22 at 07:12
  • and just in case ur thinking that the question that I mentioned is by myself and I'm just getting attention to that question too, I'm not doing that, there are answers to that question but that user also didn't get the correct answer, and neither me so I'm just mentioning that question so at least I get an answer to it – Vasant Raval Feb 04 '22 at 07:13
  • that question is different from what I asked today, I was just mentioning it because I also have the same issue, if u saw my today's question I have this line where the main error is (at least what I think is ):- seekBar2.setProgress(player2.getCurrentPosition()); but just after it I have an issue with this line too handler2.postDelayed(this, 100); , which is the question that user has asked about – Vasant Raval Feb 04 '22 at 07:18
  • and I tried to find an answer before asking it on SO but there are no solution to my exact question not even close ,ok i found the same question which is this https://github.com/johnsonsu/react-native-sound-player/issues/88 but it doesn't have any proper answer – Vasant Raval Feb 04 '22 at 07:23

1 Answers1

1

Enter this code:-

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

 MediaPlayer player, player2;
 SeekBar seekBar1, seekBar2;
 TextView currentTime1, currentTime2;
 TextView totalTime1, totalTime2;
 Handler handler1 = new Handler();
 Handler handler2 = new Handler();
 ImageView play, pause, stop, play2, pause2, stop2;
 int pauseCurrentPosition, pauseCurrentPosition2;
 UpdateSeekBar1 updateSeekBar1 = new UpdateSeekBar1();
 UpdateSeekBar2 updateSeekBar2 = new UpdateSeekBar2();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    player = MediaPlayer.create(this, R.raw.bekhayali);
    player2 = MediaPlayer.create(this, R.raw.hawabanke);
    seekBar1 = findViewById(R.id.seekbar1);
    seekBar2 = findViewById(R.id.seekbar2);
    play = findViewById(R.id.playbtn);
    pause = findViewById(R.id.pausebtn);
    stop = findViewById(R.id.stopbtn);
    play2 = findViewById(R.id.playbtn2);
    pause2 = findViewById(R.id.pausebtn2);
    stop2 = findViewById(R.id.stopbtn2);
    currentTime1 = findViewById(R.id.currentTime1);
    currentTime2 = findViewById(R.id.currentTime2);
    totalTime1 = findViewById(R.id.totalTime1);
    totalTime2 = findViewById(R.id.totalTime2);
    play.setOnClickListener(this);
    pause.setOnClickListener(this);
    stop.setOnClickListener(this);
    play2.setOnClickListener(this);
    pause2.setOnClickListener(this);
    stop2.setOnClickListener(this);
    seekBar1.setMax(player.getDuration());
    seekBar2.setMax(player2.getDuration());
    seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
                player.seekTo(seekBar.getProgress());
        }
    });
    seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            player2.seekTo(seekBar.getProgress());

        }
    });
    String totTime1 = createTimerLable1(player.getDuration());
    totalTime1.setText(totTime1);
    String totTime2 = createTimerLable2(player2.getDuration());
    totalTime1.setText(totTime2);

}

@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.playbtn:

            handler1.post(updateSeekBar1);
            if (player == null) {
                player = MediaPlayer.create(getApplicationContext(), R.raw.bekhayali);
                player.start();


            } else if (!player.isPlaying()) {
                player.seekTo(pauseCurrentPosition);
                player.start();


            }
            break;
        case R.id.pausebtn:

            if (player != null) {
                player.pause();
                pauseCurrentPosition = player.getCurrentPosition();
            }
            break;
        case R.id.stopbtn:
            if (player != null) {
                player.stop();
                handler1.removeCallbacks(updateSeekBar1);
                player = null;
            }
            break;
        case R.id.playbtn2:
            handler2.post(updateSeekBar2);
            if (player2 == null) {
                player2 = MediaPlayer.create(getApplicationContext(), R.raw.hawabanke);
                player2.start();
            } else if (!player2.isPlaying()) {
                player2.seekTo(pauseCurrentPosition2);
                player2.start();

            }
            break;
        case R.id.pausebtn2:
            if (player2 != null) {
                player2.pause();
                pauseCurrentPosition2 = player2.getCurrentPosition();
            }
            break;
        case R.id.stopbtn2:
            if (player2 != null) {
                player2.stop();
                handler2.removeCallbacks(updateSeekBar2);
                player2 = null;
            }
            break;

    }
}

public String createTimerLable1(int duration) {
    String timerLabel = "";
    int min = duration / 1000 / 60;
    int sec = duration / 1000 % 60;
    timerLabel += min + ":";
    if (sec < 10) timerLabel += "0";
    timerLabel += sec;
    return timerLabel;


}

public String createTimerLable2(int duration) {
    String timerLabel = "";
    int min = duration / 1000 / 60;
    int sec = duration / 1000 % 60;
    timerLabel += min + ":";
    if (sec < 10) timerLabel += "0";
    timerLabel += sec;
    return timerLabel;


}

public class UpdateSeekBar1 implements Runnable {

    @Override
    public void run() {
        currentTime1.setText(createTimerLable1(seekBar1.getProgress()));
        seekBar1.setProgress(player.getCurrentPosition());
        handler1.postDelayed(this, 100);

    }
}

public class UpdateSeekBar2 implements Runnable {

    @Override
    public void run() {
        currentTime2.setText(createTimerLable2(seekBar2.getProgress()));
        seekBar2.setProgress(player2.getCurrentPosition());
        handler2.postDelayed(this, 100);
    }
}}
  • thank you, brother, this thing solved my other issue with post delayed where the audio is not smooth, but still the current issue is still there the app crash when I press the stop image view and the error is the same " Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object reference " – Vasant Raval Feb 04 '22 at 07:40
  • I have an issue after removing this line player.seekTo(progress); from onProgrLessChanged I'm not able to move the seek bar (change the seek bar position manually) and even if I add it the audio issue has started again – Vasant Raval Feb 04 '22 at 08:06
  • this code is test my device. this code working properly – Bhavik Kalsariya Feb 04 '22 at 08:25