Sophie
Sophie

Reputation: 2634

MediaPlayer getting Null Pointer Exception when do tap on BACK button

Streaming Audio online but always getting NullPointerException whenever do tap on BACK button (when playing music) that time only

this is the line, where i am getting exception inside UpdateSongTime Runnable :

  startTime = mediaPlayer.getCurrentPosition(); // NullPointerException

code looks like below :

        public TextView startTimeField,endTimeField;
        private double startTime = 0;
        private double finalTime = 0;
        private SeekBar seekbar;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_music_player);

          public void play(View view){         
           mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(videoURL);
            } catch (IllegalArgumentException e) {
            } catch (SecurityException e) {
            } catch (IllegalStateException e) {
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mediaPlayer.prepare();
            } catch (IllegalStateException e) {
            } catch (IOException e) {
            }
            mediaPlayer.start();

            finalTime = mediaPlayer.getDuration();
              startTime = mediaPlayer.getCurrentPosition();
              if(oneTimeOnly == 0){
                 seekbar.setMax((int) finalTime);
                 oneTimeOnly = 1;
              } 

              endTimeField.setText(String.format("%dm:%ds", 
                 TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
                 TimeUnit.MILLISECONDS.toSeconds((long) finalTime) - 
                 TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                 toMinutes((long) finalTime)))
              );
              startTimeField.setText(String.format("%dm:%ds", 
                 TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                 TimeUnit.MILLISECONDS.toSeconds((long) startTime) - 
                 TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                 toMinutes((long) startTime)))
              );
              seekbar.setProgress((int)startTime);
              myHandler.postDelayed(UpdateSongTime,100); 


          pauseButton.setEnabled(true);
          playButton.setEnabled(false);

          mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                pauseButton.setEnabled(false);
                  playButton.setEnabled(true);
                  startTimeField.setText("0m:0s");
                  seekbar.setProgress(0);
            }
        });
       }

       private Runnable UpdateSongTime = new Runnable() {
              public void run() {
                 startTime = mediaPlayer.getCurrentPosition(); // NullPointerException

                 startTimeField.setText(String.format("%dm:%ds", 
                    TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                    TimeUnit.MILLISECONDS.toSeconds((long) startTime) - 
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
                    toMinutes((long) startTime)))
                 );
                 seekbar.setProgress((int)startTime);
                 myHandler.postDelayed(this, 100);
              }
           };

 public void pause(View view){       
          mediaPlayer.pause();
          pauseButton.setEnabled(false);
          playButton.setEnabled(true);
       }    

/** Called just before the activity is destroyed. */
        @Override
        public void onDestroy() {
           super.onDestroy();

               if(mediaPlayer!=null && mediaPlayer.isPlaying()){
                   mediaPlayer.stop();
                   mediaPlayer.release();
                   mediaPlayer = null;
                }         
        }

What i have not implemented in my code? where it requires changes in my code?

Upvotes: 1

Views: 374

Answers (1)

kalyan pvs
kalyan pvs

Reputation: 14590

You are missing removing the Handler callbacks in onDestroy() of Your Activity

Add this line in your onDestroy() method

myHandler.removeCallbacks(this);

Upvotes: 1

Related Questions