Search code examples
androidtimermedia-player

How to stop media player from another activity in Android programmatically?


I am trying to use media player in my app and I have trying to stop media player from other activity my coding is following:

FirstActivity:

public void stop() {

    if (playPause == false) {

        control.setBackgroundResource(R.drawable.play);

        mediaPlayer.stop();
        new Player().cancel(true);
        media.stop();
        media.reset();
        mediaPlayer.reset();
        if (mediaPlayer.isPlaying())
            mediaPlayer.stop();
        mediaPlayer.reset();
        media.stop();

        playPause = true;
    } else {
        control.setBackgroundResource(R.drawable.pause);
        if (intialStage) {
            new Player()
                    .execute(URL);
        } else {
            if (!mediaPlayer.isPlaying())
                mediaPlayer.start();
        }
        playPause = false;
    }
}

SecondActivity:

Have to stop media player after the timer is end:

        @Override
        public void onFinish() {
            textViewTime.setText(hmsTimeFormatter(timeCountInMilliSeconds));
            // call to initialize the progress bar values
            setProgressBarValues();
            // hiding the reset icon
            imageViewReset.setVisibility(View.GONE);
            // changing stop icon to start icon
            imageViewStartStop.setImageResource(R.drawable.icon_start);
            // making edit text editable
            editTextMinute.setEnabled(true);
            // changing the timer status to stopped
            timerStatus = TimerStatus.STOPPED;

            MainActivity main = new MainActivity();
            main.stop();
        }

    }.start();
    countDownTimer.start();
}

the above coding is shows error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
                                                                         at com.digitamatix.mukilfm.MainActivity.stop(MainActivity.java:549)

I have to stop media player in first activity please help me on my coding to fix my issues


Solution

  • see i have started activity with startActivityForResult . onActivityResult you will get result.

    startActivityForResult(Activity1.createIntent(this), 1001);
    
    
    //handle callback result
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1001) {
    
            }
        }
    
    }
    

    Below code will call in next activity where you want to callback and finish the activity. You can pass any value with this intent.

     Intent intent = new Intent();
     setResult(RESULT_OK, intent);
     finish();
    

    So, as per requirement you can use this. Stop media player. Other option local broadcast too. Hope this will help you.