为什么哔哔声会导致应用程序崩溃?
Why the beep sound makes app crashes?
我尝试制作一个水平指示器,当 phone 到达水平位置(使用加速度计)时,它会发出更快的蜂鸣声,在触摸屏幕后发出蜂鸣声,如果再次触摸屏幕则停止。
所以我编写了以下代码,我遇到的问题是,由于我添加了“哔”声部分(在 onTouch 事件中调用了 MyRunnable 函数),我的应用程序在几秒钟后崩溃(它可以正常工作几秒钟和构建后我没有错误消息)。
我真的不知道问题出在哪里。我被困在这里需要一些帮助,谢谢!
public class MainActivity 扩展 AppCompatActivity 实现 SensorEventListener,View.OnTouchListener {
Sensor mySensor;
SensorManager SM;
float ANGLEX, ANGLEY, ANGLEZ;
int aX, aY, aZ;
int roundANGLEX, roundANGLEY, roundANGLEZ;
int Xetal, Yetal;
double centre;
int Rcentre;
int Rcentre2;
boolean active;
int test;
int i=0;
private Handler myHandler;
private Runnable myRunnable = new Runnable() {
@Override
public void run() {
// Play a periodic beep which accelerates according to Rcentre2
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP,150);
myHandler.postDelayed(this,(long) Math.sqrt(Rcentre2*20)+50);
}
};
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Créée notre Sensor Manager
SM = (SensorManager) getSystemService(SENSOR_SERVICE);
//Accéléromètre
mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//Registre Sensor Listener
SM.registerListener(this, mySensor, 1000000, 1000000);
((ConstraintLayout) findViewById(R.id.layout_main)).setOnTouchListener((View.OnTouchListener) this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
//Pas utilisé
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float z_stable = ((int) ((sensorEvent.values[2]) * 100)) / 100.0f;
ANGLEX = (float) (((float) (Math.acos(sensorEvent.values[0] / 9.807)) * (180 / Math.PI))); //I get the accelerometer's values
ANGLEY = (float) (((float) (Math.acos(sensorEvent.values[1] / 9.807)) * (180 / Math.PI)));
ANGLEZ = (float) (((float) (Math.acos(z_stable / 9.807)) * (180 / Math.PI)));
roundANGLEX = Math.round(ANGLEX);
roundANGLEY = Math.round(ANGLEY);
roundANGLEZ = Math.round(ANGLEZ);
aX = roundANGLEX;
aY = roundANGLEY;
aZ = roundANGLEZ;
Xetal = aX - 88; //Xetal and Yetal are 0 when phone is on plane surface
Yetal = aY - 90; //and go from -90 to +90
centre = Math.sqrt(Xetal * Xetal + Yetal * Yetal); //gives the "distance" from the "center => the smaller centre gets, the closer the phone approach horizontal
Rcentre = (int) Math.round(centre);
Rcentre2 = (int) Math.round(centre * 100);
}
public boolean onTouch(View view, MotionEvent motionEvent) {
if (active == true) {
active = false;
myHandler.removeCallbacks(myRunnable);
}
else if (active == false) {
active = true;
myHandler = new Handler();
myHandler.postDelayed(myRunnable,0);
}
return false;
}
}
这里是Logcat的信息,好像显示出了一些问题,但我不知道那是什么意思。
logcat information
我在
找到了适合我的答案
"it was just about releasing created objects of ToneGenerator because rapidly creating objects of 'ToneGenerator' without releasing them will cause the application to crash."
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP,150);
toneGen1.release();
我加toneGen1.release();现在可以正常使用了。
感谢 Mahmoud Farahat。
使用似乎有效的信号量 (tonebusy) (KOTLIN) :
var tonebusy : Boolean = false
fun beep(i : Int) {
if (tonebusy) {
return
}
else
{
tonebusy = true
val toneGen1 = ToneGenerator(AudioManager.STREAM_MUSIC, 100)
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, i)
toneGen1.release();
}
tonebusy = false
}
我尝试制作一个水平指示器,当 phone 到达水平位置(使用加速度计)时,它会发出更快的蜂鸣声,在触摸屏幕后发出蜂鸣声,如果再次触摸屏幕则停止。 所以我编写了以下代码,我遇到的问题是,由于我添加了“哔”声部分(在 onTouch 事件中调用了 MyRunnable 函数),我的应用程序在几秒钟后崩溃(它可以正常工作几秒钟和构建后我没有错误消息)。 我真的不知道问题出在哪里。我被困在这里需要一些帮助,谢谢!
public class MainActivity 扩展 AppCompatActivity 实现 SensorEventListener,View.OnTouchListener {
Sensor mySensor;
SensorManager SM;
float ANGLEX, ANGLEY, ANGLEZ;
int aX, aY, aZ;
int roundANGLEX, roundANGLEY, roundANGLEZ;
int Xetal, Yetal;
double centre;
int Rcentre;
int Rcentre2;
boolean active;
int test;
int i=0;
private Handler myHandler;
private Runnable myRunnable = new Runnable() {
@Override
public void run() {
// Play a periodic beep which accelerates according to Rcentre2
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP,150);
myHandler.postDelayed(this,(long) Math.sqrt(Rcentre2*20)+50);
}
};
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Créée notre Sensor Manager
SM = (SensorManager) getSystemService(SENSOR_SERVICE);
//Accéléromètre
mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//Registre Sensor Listener
SM.registerListener(this, mySensor, 1000000, 1000000);
((ConstraintLayout) findViewById(R.id.layout_main)).setOnTouchListener((View.OnTouchListener) this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
//Pas utilisé
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float z_stable = ((int) ((sensorEvent.values[2]) * 100)) / 100.0f;
ANGLEX = (float) (((float) (Math.acos(sensorEvent.values[0] / 9.807)) * (180 / Math.PI))); //I get the accelerometer's values
ANGLEY = (float) (((float) (Math.acos(sensorEvent.values[1] / 9.807)) * (180 / Math.PI)));
ANGLEZ = (float) (((float) (Math.acos(z_stable / 9.807)) * (180 / Math.PI)));
roundANGLEX = Math.round(ANGLEX);
roundANGLEY = Math.round(ANGLEY);
roundANGLEZ = Math.round(ANGLEZ);
aX = roundANGLEX;
aY = roundANGLEY;
aZ = roundANGLEZ;
Xetal = aX - 88; //Xetal and Yetal are 0 when phone is on plane surface
Yetal = aY - 90; //and go from -90 to +90
centre = Math.sqrt(Xetal * Xetal + Yetal * Yetal); //gives the "distance" from the "center => the smaller centre gets, the closer the phone approach horizontal
Rcentre = (int) Math.round(centre);
Rcentre2 = (int) Math.round(centre * 100);
}
public boolean onTouch(View view, MotionEvent motionEvent) {
if (active == true) {
active = false;
myHandler.removeCallbacks(myRunnable);
}
else if (active == false) {
active = true;
myHandler = new Handler();
myHandler.postDelayed(myRunnable,0);
}
return false;
}
}
这里是Logcat的信息,好像显示出了一些问题,但我不知道那是什么意思。 logcat information
我在
"it was just about releasing created objects of ToneGenerator because rapidly creating objects of 'ToneGenerator' without releasing them will cause the application to crash."
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP,150);
toneGen1.release();
我加toneGen1.release();现在可以正常使用了。
感谢 Mahmoud Farahat。
使用似乎有效的信号量 (tonebusy) (KOTLIN) :
var tonebusy : Boolean = false
fun beep(i : Int) {
if (tonebusy) {
return
}
else
{
tonebusy = true
val toneGen1 = ToneGenerator(AudioManager.STREAM_MUSIC, 100)
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, i)
toneGen1.release();
}
tonebusy = false
}