I am trying to use the azimuth measurements of a compass to change the volume. An unknown issue occurs whenever I use anything related to the volume where, when I run the app, it opens and closes immediately. I am currently setting the volume to zero, as I do not know the range of allowable volumes yet. The code should map the azimuth angles (0-360) to a volume percentage (0-100). The idea is to play music where the sound changes with the angle.
package com.example.compass;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;
import android.media.AudioManager;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private ImageView imageView;
private float[] mGravity = new float[3];
private float[] mGeomagnetic = new float[3];
private float azimuth = 0f;
private float currectAzimuth = 0f;
private SensorManager mSensorManager;
private TextView mTextViewResult;
private AudioManager audioManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextViewResult = findViewById(R.id.textview_result);
imageView = (ImageView)findViewById(R.id.compass);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume(){
super.onResume();
mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause(){
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent){
final float alpha = 0.97f;
synchronized (this){
if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
mGravity[0] = alpha*mGravity[0]+(1-alpha)*sensorEvent.values[0];
mGravity[1] = alpha*mGravity[1]+(1-alpha)*sensorEvent.values[1];
mGravity[2] = alpha*mGravity[2]+(1-alpha)*sensorEvent.values[2];
}
if(sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
{
mGeomagnetic[0] = alpha*mGeomagnetic[0]+(1-alpha)*sensorEvent.values[0];
mGeomagnetic[1] = alpha*mGeomagnetic[1]+(1-alpha)*sensorEvent.values[1];
mGeomagnetic[2] = alpha*mGeomagnetic[2]+(1-alpha)*sensorEvent.values[2];
}
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R,I,mGravity,mGeomagnetic);
if (success){
float orientation[] = new float[3];
SensorManager.getOrientation(R,orientation);
azimuth = (float)Math.toDegrees(orientation[0]);
azimuth = (azimuth+360)%360;
//
Animation anim = new RotateAnimation(-currectAzimuth,-azimuth, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
currectAzimuth = azimuth;
mTextViewResult.setText(String.valueOf(Math.round(currectAzimuth)));
//anything related to the volume breaks the program
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
Math.round(0), 0);
//
anim.setDuration(500);
anim.setRepeatCount(0);
anim.setFillAfter(true);
imageView.startAnimation(anim);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i){
}
}
The problem has been resolved. I believe the issue was the virtual device, which I reinstalled.