IOS无法调用摄像头和陀螺仪的问题处理

浏览器调用摄像头

private createVideo(): void {
        this.div = document.createElement('div');

        this.div.innerHTML = '<video id="videoElement" class="videoElement"  autoplay="autoplay"  muted="muted"   playsinline style="width: 100vw;height:100vh;object-fit: cover"></video>'
        this.div.style = "position:absolute;background-color: transparent; z-order:1;";

        document.body.appendChild(this.div);

        this.video = document.getElementById('videoElement') as HTMLVideoElement;

        // 打开摄像头并获取视频流
        navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: 'environment' } } })
            .then((stream) => {
                this.video.srcObject = stream;
                this.video.play();// IOS掉不起摄像头是因为少了这一句。一定要加
            })
            .catch((error) => {
                console.error('无法访问摄像头:', error);
                this.video = null
                document.body.removeChild(this.div)
            });
    }

浏览器调用陀螺仪

if (window.DeviceOrientationEvent !== undefined && typeof window.DeviceOrientationEvent.requestPermission === 'function') {
	window.DeviceOrientationEvent.requestPermission().then(function (response) {
			if (response == 'granted') {
				window.addEventListener('orientationchange', onScreenOrientationChangeEvent);
				window.addEventListener('deviceorientation', onDeviceOrientationChangeEvent);
			}
		}).catch(function (error) {
			console.error('DeviceOrientationControls: Unable to use DeviceOrientation API:', error);
		});
	} else {
		window.addEventListener('orientationchange', onScreenOrientationChangeEvent);
		window.addEventListener('deviceorientation', onDeviceOrientationChangeEvent);
	}

注意:IOS调用陀螺仪必须加按钮手动触发才能用调用起来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值