0

When the Media player plays the sound and each time orientation of the phone is changed from Portrait to Landscape or Landscape to Portrait, the sound starts again each time orientation is changed.

How do I continue the same sound which is currently playing in other orientation, without playing a new sound?

Here is my code:

public void SoundStuff(final Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewpager_example);

    mP = MediaPlayer.create(this, R.raw.c101);

    //btn1 = (Button) findViewById(R.id.btnPlay);
    tgbtn1 = (ToggleButton) findViewById(R.id.imageButton2);
    btnstop1 = (Button) findViewById(R.id.button1);

    tgbtn1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

                // The toggle is enabled
                 try {
                     mP.start();
                      //tgbtn1.setVisibility(View.GONE);

                 }
                 catch (Exception exception) {
                     Log.v("exception:play", exception.toString());
                 }
             }
             else {

                 // The toggle is disabled
                 try {
                     mP.pause();
                 }
                 catch (Exception exception) {
                     Log.v("exception:pause", exception.toString());
                 }
             }
         }
     });


     btnstop1.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
             if(mP.isPlaying()){
                 mP.stop();
             }

             SoundStuff(savedInstanceState);
             ZoomImageView mViewPager = (ZoomImageView) findViewById(R.id.view_pager);

             Btn1ImageAdapter adapter = new Btn1ImageAdapter();
             mViewPager.setAdapter(adapter);
         }
     });
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Apps Vine
  • 3
  • 2

1 Answers1

0

Well, activity will be recreated when the orientation is changed so you can manually handle the orientation changes by adding the below line in your activity in the manifest file,

android:configChanges="orientation|screenSize"

This will prevent your activity when recreating and will simply resume when orientation is changed. Try this.

When you are using the above solution you don't need to specially define your layout for landscape mode.

But if you need to define it then to work in landscape & portrait mode you can simply define those change in the below method.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
krishank Tripathi
  • 616
  • 1
  • 7
  • 23
  • happy to help but also add this line to all activity where your activity will be in use of any service or other if orientation is changed – krishank Tripathi Apr 28 '18 at 09:48
  • Thank you. That solves the sound problem but is not calling the res/layout-land layouts for landscape activities, instead its displaying the layouts from res/layout – Apps Vine Apr 28 '18 at 09:54
  • Now the orientation issue is fixed and is calling the layouts but the sound plays again and again each time the orientation is changed. The old sound is playing while a new sound is played each time the orientation changes – Apps Vine Apr 28 '18 at 10:39
  • check this out https://stackoverflow.com/questions/17917994/how-to-play-audio-continuously-while-orientation-changes-in-android – krishank Tripathi Apr 28 '18 at 11:10