-2

I'm building a simple app, when the program starts the text blink and the song is played. When I rotate my phone it should change the landscape and restart the song. I'm able to do the first part but it crashes when I change the landscape. It works if i remove the onPause method but when i do that music keeps on playing in the background and it also starts from the beginning. There is two songs playing at the same time.

 import android.media.MediaPlayer;
 import android.os.Bundle;
 import android.os.Handler;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
MediaPlayer ourSong;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ourSong = MediaPlayer.create(this,R.raw.fixyou);
    ourSong.start();
    blinkText();
}

 @Override
protected void onPause(){
    super.onPause();
    ourSong.release();
    finish();
}

private void blinkText() {
    // TODO Auto-generated method stub
    final Handler handler = new Handler();
    new Thread(new Runnable() {
        @Override
        public void run() {
            int timeToBlink = 500;    //in ms
            try{
                Thread.sleep(timeToBlink);
            }catch (Exception e) {

            }
            handler.post(new Runnable() {
                @Override
                public void run() {
                    TextView txt = (TextView) findViewById(R.id.tv);
                    if(txt.getVisibility() == View.VISIBLE){
                        txt.setVisibility(View.INVISIBLE);
                    }else{
                        txt.setVisibility(View.VISIBLE);
                    }
                    blinkText();
                }
            });
        }}).start();



}
}
  • You need to handle onconfigurationchanged – surya Sep 20 '16 at 01:14
  • @surya where do i do that, is that the new action listener that i need to create. im a newbies on this really appreciate your help – user2905259 Sep 20 '16 at 01:16
  • check this http://stackoverflow.com/questions/17917994/how-to-play-audio-continuously-while-orientation-changes-in-android – surya Sep 20 '16 at 01:17

1 Answers1

1
 android:configChanges="orientation|screenSize"

use this line in your manifest file in your desired activity tag like this

<activity
android:name=".PActivity"
android:label="@string/title_activity_r"
android:noHistory="true"
android:configChanges="orientation|screenSize">
</activity>
  • i was trying to see if there is a way to do it without touching manifest file – user2905259 Sep 20 '16 at 18:13
  • as @surya said please see this answer it will solve your problem http://stackoverflow.com/questions/17917994/how-to-play-audio-continuously-while-orientation-changes-in-android – Ashish Mathur Sep 21 '16 at 05:28