skip to Main Content

I want to set a autoplay sound and it only play once, no matter the rotation is. I was trying to make my Mediaplayer in the onCreate of Mainactivity. And when I try to rotate the screen, the audio continued to play.

I try a different method like set up a subclass and call in the activity instead of puting it in onCreat of MainActivity to make it only play once and ignore doing rotate, but it still doesn’t work, can someone explain to me how can I fix it? Thank you!

2

Answers


  1. You should clerify your case with code, but seams that it should help

    @Override
    protected void onStop() {
        super.onStop();
        player.release();
        player = null;
    }
    
    Login or Signup to reply.
  2. You should use onConfigurationChanged to make this work.

    in your manifest file add this to your activiy

     android:configChanges="orientation|keyboardHidden"
    
    <activity android:name=".MyActivity"
              android:configChanges="orientation|keyboardHidden"
              android:exported="true">
    

    In your MainActiviy code implement this method:

    @Override
    public void onConfigurationChanged(@NotNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // check weather the player is null
        if(player == null) return;
        player.stop();
       
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search