java - MediaPlayer plays double sounds on change orientation -
i have big problem app. problem when orientation changes landscape , change portrait music twice tracks plays @ same time. when start app on portrait have no problem it.
package com.phone.sensor; import android.app.activity; import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener; import android.hardware.sensormanager; import android.media.mediaplayer; import android.os.bundle; import android.view.menu; import android.widget.textview; public class sensoractivity extends activity implements sensoreventlistener{ public boolean musstatus = false; public boolean musdeclare = true; public mediaplayer mp = null; sensor accelerometer; sensormanager sm; textview acceleration; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if(savedinstancestate == null){ if(mp != null){ mp.stop(); mp.reset(); mp = null; } } else { if(mp != null){ mp.stop(); mp.reset(); mp = null; } } setcontentview(r.layout.activity_main); sm=(sensormanager) getsystemservice(sensor_service); accelerometer=sm.getdefaultsensor(sensor.type_accelerometer); sm.registerlistener(this, accelerometer, sensormanager.sensor_delay_normal); acceleration=(textview)findviewbyid(r.id.acceleration); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public void onaccuracychanged(sensor sensor, int accuracy) { // todo auto-generated method stub } @override public void onsaveinstancestate (bundle outstate) { super.onsaveinstancestate(outstate); outstate.putboolean("musstatus", musstatus); outstate.putboolean("musdeclare", musdeclare); } public object onretaincustomnonconfigurationinstance() { return this.mp; } @override public void onsensorchanged(sensorevent event) { // todo auto-generated method stub acceleration.settext("x: "+event.values[0]+ "\ny: "+event.values[1]+ "\nz: "+event.values[2]); float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; if(y > 8.9) { if(musdeclare == true) { mp = mediaplayer.create(this, r.raw.alexander); musdeclare = false; } if(musstatus == false) { mp.start(); musstatus = true; } } if(y < 5) { if(musstatus == true) { mp.stop(); mp.reset(); mp = null; musstatus = false; musdeclare = true; } } } }
try putting block of code in onpause
method of class
if(mp != null && mp.isplaying()){ mp.stop(); mp.reset(); mp = null; } }
Comments
Post a Comment