android 如何防止来电时停止录像?

dtcbnfnu  于 2023-09-28  发布在  Android
关注(0)|答案(1)|浏览(98)

我正在使用CameraX在我的应用程序中录制视频。通常视频拍摄需要15-20分钟,我们在应用程序中有控件来暂停和恢复视频拍摄。现在我面临的问题是,当有来电时,视频捕捉停止而不是暂停。如果视频捕获停止,用户将不得不重新拍摄视频,这是一个巨大的不便。如果暂停,用户可以在通话后恢复。当有来电时,我可以在应用程序中获得任何回调,以便我可以手动将视频捕获设置为暂停模式?或者还有其他可能性?
更新:当我尝试Hazy Ziv提供的解决方案(不推荐的代码)时,我能够获得来电的回调,并将视频设置为暂停状态。但是,当电话被接听时,我收到VideoRecordEvent。完成CameraX的回调。所以不管我是否暂停视频,视频捕获停止并返回URI:(

vmdwslir

vmdwslir1#

TelephonyManager处理来电
在视频活动注册事件侦听器中

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String phoneNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // Incoming call: Pause video capture here
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                // No call: Resume video capture here if it was paused
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                // A call is dialing, active or on hold
                break;
            default:
                break;
        }
    }
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

不要忘记取消注册监听器

@Override
protected void onDestroy() {
    super.onDestroy();
    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}

喷气背包合成器

@Composable
fun ListenToPhoneState(onCallStateChanged: (Int, String) -> Unit) {
    val context = LocalContext.current
    val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

    val currentOnCallStateChanged by rememberUpdatedState(onCallStateChanged)

    DisposableEffect(Unit) {
        val phoneStateListener = object : PhoneStateListener() {
            override fun onCallStateChanged(state: Int, phoneNumber: String) {
                currentOnCallStateChanged(state, phoneNumber)
            }
        }

        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)

        onDispose {
            telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
        }
    }
}

在您的Ui中收听

@Composable
fun YourScreen() {
    ListenToPhoneState { state, phoneNumber ->
        when (state) {
            TelephonyManager.CALL_STATE_RINGING -> {
                // Incoming call: Pause video capture here
            }
            TelephonyManager.CALL_STATE_IDLE -> {
                // No call: Resume video capture here if it was paused
            }
            TelephonyManager.CALL_STATE_OFFHOOK -> {
                // A call is dialing, active or on hold
            }
        }
    }

    // Your other composables and UI elements
}

相关问题