2014-05-01 93 views
0

我能夠開始播放第一首歌曲,一旦檢測到移動,它就會變成另一首歌曲,但是,如果我停止移動,它不會再次開始第一首歌曲。我應該循環嗎?還是有什麼我失蹤?如何播放在Android中調用的第一首歌曲?

這裏是我的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(); 
    } 

} 

回答

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

謝謝,但我試過了,仍然一無所獲。 eclipse中的logcat正在循環回調錯誤,但我不確定它出錯的地方或原因。第一首後,它不會播放第二首歌。如果震動停止,第一個不會再次播放。 –

相關問題