我正在开发VOIP应用程序,并使用PROXIMITY_SCREEN_OFF_WAKE_LOCK唤醒锁,如下所示。
我注意到,在许多设备上,当接近传感器打开并且屏幕变黑时,我的应用程序的音频播放将停止。 HTC,LG和华为设备上会发生这种情况。
为了防止在触发传感器时音频播放停止,还需要做些其他事情吗?我在其他VOIP应用程序(例如Skype)上注意到了相同的问题。
int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 32;
mProximityWakeLock = pm.newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "myLock");
if(!mProximityWakeLock.isHeld()){
mProximityWakeLock.acquire();
}
非常感谢!
最佳答案
我们遇到了同样的问题。我们真正检测到的是,当WakeLock关闭屏幕时,输入套接字停止接收数据,因此我们向AudioTrack发送了零。
为了改善这一点,我们退出使用PROXIMITY_SCREEN_OFF_WAKE_LOCK,现在使用SensorManager(我们研究了Linphone代码):
public class InCallActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mProximity;
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
final Window window = getWindow();
WindowManager.LayoutParams lAttrs = getWindow().getAttributes();
View view = ((ViewGroup) window.getDecorView().findViewById(android.R.id.content)).getChildAt(0);
if (event.values[0] > 4) {
// turn on screen
Log.v("LlamadaActivity", "Encendemos pantalla");
lAttrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
view.setVisibility(View.VISIBLE);
}
else {
// turn off screen
Log.v("LlamadaActivity", "Apagamos pantalla");
lAttrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
view.setVisibility(View.INVISIBLE);
}
window.setAttributes(lAttrs);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
// Para que la pantalla no se apague, y que la encienda aunque este apagada y bloqueada
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//
}
@Override
protected void onDestroy() {
super.onDestroy();
mSensorManager.unregisterListener(this);
}
}