1

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

非常感谢!

4

1 回答 1

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);
    }
}
于 2012-10-05T09:18:04.640 回答