简体   繁体   中英

Sound plays over and over when each time mobile orientation is changed

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);
         }
     });

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM