如何播放第一首我在Android中调用的第一首歌?

时间:2014-05-01 01:18:39

标签: java android loops android-mediaplayer sensor

我能够开始第一首歌,在检测到动作后,它将变为另一首歌,但是,如果我停止动作,它不会再次开始第一首歌。我应该循环吗?或者有什么我想念的东西?

这是 MainActivity.java

package com.capisrem.manofsteel;

import android.content.Context;
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.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {
    private SensorManager mSensorManager;
    private MediaPlayer mp1, mp2;
    private float mAccel; // acceleration apart from gravity
    private float mAccelCurrent; // current acceleration including gravity
    private float mAccelLast; // last acceleration including gravity

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;

        // Intro
        mp1 = MediaPlayer.create(MainActivity.this, R.raw.intro);
        // Add OnCompletionListener to release the
        mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp1) {
                mp1.start();
            }

        });

        // Saving the World!
        mp2 = MediaPlayer.create(MainActivity.this, R.raw.savingtheworld);
    }

    private final SensorEventListener mSensorListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent se) {

            float x = se.values[0];
            float y = se.values[1];
            float z = se.values[2];
            mAccelLast = mAccelCurrent;
            mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
            float delta = mAccelCurrent - mAccelLast;
            mAccel = mAccel * 0.9f + delta; // perform low-cut filter

            if (mAccel > 8) {
                if (mp1.isPlaying()) {
                    mp1.stop();
                }
                mp2.start();
            }

            mp1.start();

        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        mSensorManager.unregisterListener(mSensorListener);
        super.onPause();
    }

}

1 个答案:

答案 0 :(得分:0)

public void onSensorChanged(SensorEvent se) {

    float x = se.values[0];
    float y = se.values[1];
    float z = se.values[2];

    mAccelLast = mAccelCurrent;
    mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));

    float delta = mAccelCurrent - mAccelLast;
    mAccel = mAccel * 0.9f + delta; // perform low-cut filter

    if (mAccel > 8) {
        if (mp1.isPlaying()) {
            mp1.stop();
        }
        mp2.start();
    }
    m2.stop(); // **try to add this line**
    mp1.start();
}