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