PROXIMITY_SCREEN_OFF_WAKE_LOCK导致音频播放问题

时间:2012-08-28 04:24:01

标签: android

我正在开发一个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();
}

非常感谢!

1 个答案:

答案 0 :(得分:8)

我们遇到了同样的问题。我们真正检测到的是当屏幕因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);
    }
}