我正在尝试制作一个当设备向上/向下/向左/向右倾斜时会根据倾斜方向播放歌曲的应用程序。
我试图重新组织循环并更改值,但是到目前为止,它们都没有起作用。目前,该应用冻结了一段时间,然后不停地播放一首歌曲。
public class TiltActivity extends AppCompatActivity implements SensorEventListener {
private ImageView mSpotTop;
private ImageView mSpotBottom;
private ImageView mSpotLeft;
private ImageView mSpotRight;
private float[ ] mAccelerometerData = new float[3];
private float[] mMagnetometerData = new float[3];
// System sensor manager instance.
private SensorManager mSensorManager;
// Accelerometer and magnetometer sensors, as retrieved from the
// sensor manager.
private Sensor mSensorAccelerometer;
private Sensor mSensorMagnetometer;
// TextViews to display current sensor values.
private TextView mTextSensorAzimuth;
private TextView mTextSensorPitch;
private TextView mTextSensorRoll;
private boolean isMusic = false;
// Very small values for the accelerometer (on all three axes) should
// be interpreted as 0. This value is the amount of acceptable
// non-zero drift.
private static final float VALUE_DRIFT = 0.05f;
MusicHelper musicHelper;
DataBase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tilt);
musicHelper = new MusicHelper();
mDatabase = new DataBase(this);
// Lock the orientation to portrait (for now)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mTextSensorAzimuth = (TextView) findViewById(R.id.value_azimuth);
mTextSensorPitch = (TextView) findViewById(R.id.value_pitch);
mTextSensorRoll = (TextView) findViewById(R.id.value_roll);
// Get accelerometer and magnetometer sensors from the sensor manager.
// The getDefaultSensor() method returns null if the sensor
// is not available on the device.
mSensorManager = (SensorManager) getSystemService(
Context.SENSOR_SERVICE);
mSensorAccelerometer = mSensorManager.getDefaultSensor(
Sensor.TYPE_ACCELEROMETER);
mSensorMagnetometer = mSensorManager.getDefaultSensor(
Sensor.TYPE_MAGNETIC_FIELD);
mSpotTop =(ImageView)findViewById(R.id.spot_top);
mSpotBottom =(ImageView)findViewById(R.id.spot_bottom);
mSpotLeft =(ImageView)findViewById(R.id.spot_left);
mSpotRight =(ImageView)findViewById(R.id.spot_right);
}
/**
* Listeners for the sensors are registered in this callback so that
* they can be unregistered in onStop().
*/
@Override
protected void onStart() {
super.onStart();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Listeners for the sensors are registered in this callback and
// can be unregistered in onStop().
//
// Check to ensure sensors are available before registering listeners.
// Both listeners are registered with a "normal" amount of delay
// (SENSOR_DELAY_NORMAL).
if (mSensorAccelerometer != null) {
mSensorManager.registerListener(this, mSensorAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
if (mSensorMagnetometer != null) {
mSensorManager.registerListener(this, mSensorMagnetometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onStop() {
super.onStop();
// Unregister all sensor listeners in this callback so they don't
// continue to use resources when the app is stopped.
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
int sensorType = sensorEvent.sensor.getType();
switch(sensorType) {
case Sensor.TYPE_ACCELEROMETER:
mAccelerometerData = sensorEvent.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mMagnetometerData = sensorEvent.values.clone();
break;
default:
return;
}
float[] rotationMatrix = new float[9];
boolean rotationOK = SensorManager.getRotationMatrix(rotationMatrix, null, mAccelerometerData, mMagnetometerData);
float orientationValues[] = new float[3];
if(rotationOK){
SensorManager.getOrientation(rotationMatrix, orientationValues);
}
float azimuth = orientationValues[0];
float pitch = orientationValues[1];
float roll = orientationValues [2];
if(Math.abs(pitch)<VALUE_DRIFT){
pitch =0;
}
if(Math.abs(roll)<VALUE_DRIFT){
roll =0;
}
mTextSensorAzimuth.setText(getResources().getString(R.string.value_format,azimuth));
mTextSensorPitch.setText(getResources().getString(R.string.value_format,pitch));
mTextSensorRoll.setText(getResources().getString(R.string.value_format,roll));
mSpotTop.setAlpha(0f);
mSpotBottom.setAlpha(0f);
mSpotLeft.setAlpha(0f);
mSpotRight.setAlpha(0f);
if(pitch >0.4){
if(isMusic == true && pitch<0.4){
musicHelper.endMusic();
isMusic = false;
} else if(isMusic == false) {
// mSpotBottom.setAlpha(pitch);
musicHelper.setMusic(getSong().get(0));
musicHelper.playMusic();
isMusic = true;
}
}
else if(pitch < -0.4){
if(isMusic == true && pitch>-0.4){
musicHelper.endMusic();
isMusic = false;
} else if(isMusic == false) {
// mSpotTop.setAlpha(Math.abs(pitch));
musicHelper.setMusic(getSong().get(3));
musicHelper.playMusic();
isMusic = true;
}
}
if(roll >0.4){
if(isMusic == true && roll<0.4){
musicHelper.endMusic();
isMusic = false;
} else if( isMusic == false) {
//mSpotLeft.setAlpha(100);
musicHelper.setMusic(getSong().get(1));
musicHelper.playMusic();
isMusic = true;
}
}
else if(roll<-0.4) {
if (isMusic == true && roll > -0.4) {
musicHelper.endMusic();
isMusic = false;
} else if (isMusic == false) {
// mSpotRight.setAlpha(100);
musicHelper.setMusic(getSong().get(2));
musicHelper.playMusic();
isMusic = true;
}
}
}
/**
* Must be implemented to satisfy the SensorEventListener interface;
* unused in this app.
*/
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
public List<String> getSong() {
Cursor data = mDatabase.getData();
ArrayList<String> listData = new ArrayList<>();
while (data.moveToNext()) {
listData.add(data.getString(1));
}
return listData;
}
}
编辑:代码被卡在循环中,因此完全没有错误