1

I developed an app that plays audio in the background. The problem I have is that when the screen orientation changes the audio stops for a few milliseconds and then continues. Here is the relevant code:

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Integer images[]={R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic6,....   

    private int currImage=0;
    MediaPlayer mp;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("currImageSaved", currImage);

        outState.putInt("time", (int)mp.getCurrentPosition());
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        int last_pos = savedInstanceState.getInt("time");
        mp.seekTo(last_pos);
        mp.start();
     }

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState  != null)
        {
            currImage = savedInstanceState.getInt("currImageSaved", 0);
        }

        mp=MediaPlayer.create(this,R.raw.shlomoimagine);

        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.main_p);
        } else {
            setContentView(R.layout.main_l);
        }

        initializeImageSwitcher();
        setInitialImage();
        setImageRotateListener();
    }


    @Override
    protected void onPause() {
        super.onPause();
        mp.pause();

    }

    private void initializeImageSwitcher(){.....
    ......}

    private void setImageRotateListener() {......
    ....}

    private void setInitialImage() {....
    ....}

    private void setCurrentImage() {....
    ...}

    @Override
    protected void onResume() {
        super.onResume();
        mp.start();

    }
    ...
}
Daniel
  • 2,355
  • 9
  • 23
  • 30
Nati Levi
  • 37
  • 5

1 Answers1

0

You can use Service for playing music on background. It won't recreated after orientation change. Or you can add

android:configChanges="orientation|screenSize|keyboardHidden"

into your manifest.xml for prevent activity recreation when orientation changes. But this time you should manually inflate new layout inside onConfigurationChanged method.

ekilic
  • 83
  • 8
  • I spent few hours to implement Service for the media player, the audio indeed was continuously playing when screen was oriented but there are two problems that cannot be ignored: first, the audio didn't stop when app was terminated and other problem is that when I used other application while this one moved to the background the music was still playing. I developed my app to stop playing when changing to other app and keep playing from the same point it was stopped when back. This is working great... But still, the problem is the short temporary audio stop. – Nati Levi Jul 04 '17 at 18:52
  • Are there any delay on starting music or stopping music? I did not understand the problem – ekilic Jul 04 '17 at 19:02
  • The problem is that when screen orientation occurs the music stop for short period of time and then continue. I don't know why and I want is to play continuously when screen oriented without the annoying stop. – Nati Levi Jul 04 '17 at 19:23
  • I think you wrote logic for stopping music in onStop or onPause. You can check isFinishing() value. If is true means that activity will be destroyed, if is false means that activity will be recreated after orientation change. So if isFinishing true do your logic otherwise dont stop music. – ekilic Jul 04 '17 at 19:47
  • Please see that I just have mp.pause(); in 'onPause()' method. – Nati Levi Jul 04 '17 at 21:24
  • Note that in this app version I started to handle two layouts R.layout.main_p and R.layout.main_l and the problem appears. In my first app version where I didn't handle two layouts but one there was no such audio problem. – Nati Levi Jul 04 '17 at 21:36