MediaPlayer在更改方向上播放双重声音

时间:2013-10-06 18:49:40

标签: java android media-player orientation

这个应用程序存在很大问题。问题在于,当方向变为横向然后将其更改为肖像时,音乐会同时播放两次音轨。但是,当我在肖像上启动应用程序时,我没有任何问题。

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 the menu; this adds items to the action bar if it is 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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

尝试将此代码块放在班级的OnPause方法

      if(mp != null && mp.IsPlaying()){
            mp.stop();
            mp.reset();
            mp = null;
        }
    }