首页 > 其他分享 >HTML5摇一摇以及音频播放问题优化总结

HTML5摇一摇以及音频播放问题优化总结

时间:2022-08-20 21:24:09浏览次数:113  
标签:acceleration info audio last 音频 HTML5 var 摇一摇

1. 摇一摇不够灵敏、摇动很多次没有响应的问题、
原来摇一摇代码是从网络Copy的,活动上线后,发现部分手机摇一摇监测效果不够灵敏,摇动很多次都没有响应,恨不得把手机砸了,于是优化。

原摇一摇代码:

var SHAKE_THRESHOLD = 800;
var last_update = 0;
var x = y = z = last_x = last_y = last_z = 0;

function deviceMotionHandler(eventData) {
var acceleration = eventData.accelerationIncludingGravity;
var curTime = new Date().getTime();

if ((curTime - last_update) > 500) {
var diffTime = curTime - last_update;
last_update = curTime;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
var status = document.getElementById("status");

if (speed > SHAKE_THRESHOLD) {
alert('摇一摇显示');
}

last_x = x;
last_y = y;
last_z = z;
}
}

if(window.DeviceMotionEvent) {
// Mobile browser support motion sensing events
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
// Mobile browser does not support the motion sensing events
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
于是开始研究起上面的代码不够灵敏的原因,发现:

The device motion event is a superset of the device orientation event; it returns data about the rotation information and also acceleration information about the device. The acceleration data is returned in three axes: x, y and z. They are measured in meters per second squared (m/s^2). Because some devices might not have the hardware to exclude the effect of gravity, the event returns two properties, accelerationIncludingGravity and acceleration, which excludes the effects of gravity, (when this is the case, the acceleration data will be null)

原来HTML5对设备移动有两个加速度相关的数据:

// Grab the acceleration from the results
var acceleration = eventData.acceleration;
info = xyz.replace("X", acceleration.x);
info = info.replace("Y", acceleration.y);
info = info.replace("Z", acceleration.z);
document.getElementById("moAccel").innerHTML = info;

// Grab the acceleration including gravity from the results
acceleration = eventData.accelerationIncludingGravity;
info = xyz.replace("X", acceleration.x);
info = info.replace("Y", acceleration.y);
info = info.replace("Z", acceleration.z);
document.getElementById("moAccelGrav").innerHTML = info;
1
2
3
4
5
6
7
8
9
10
11
12
13
于是,优化后代码如下:

var SHAKE_THRESHOLD = 300,
last_update = 0,
x = y = z = last_x = last_y = last_z = 0,

function deviceMotionHandler(eventData) {
var acceleration = eventData.accelerationIncludingGravity;
var curTime = new Date().getTime();

if ((curTime - last_update) > 500) { //多次移动事件中取两个点的事件间隔
var diffTime = curTime - last_update;
last_update = curTime;

x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
//var speed = Math.abs(x + y + z - last_x - last_y - last_z) / (diffTime * 1000);

//主要优化点1:原来的计算方式把x、y、z三方向的移动有可能会互相干扰。比如x往真方向,y往负方向,就互相抵消了。
var dist = Math.sqrt((x-last_x)*(x-last_x)+(y-last_y)*(y-last_y)+(z-last_y)*(z-last_y))
var speed = dist/diffTime*10000;

//优化点2:摇动速度测试调整,达到最优
if (speed > SHAKE_THRESHOLD) { //摇一摇灵敏度
alert('摇一摇显示');
}
last_x = x;
last_y = y;
last_z = z;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
参考链接:http://www.html5rocks.com/en/tutorials/device/orientation/

2.页面报WARNING:The devicemotion event is deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS.
上面的 devicemotion发现会报如上警告,查了一些资料,目前木有办法解决,除非切换到https。

3. ERROR: Uncaught (in promise) DOMException: The element has no supported sources.错误
原来的插入audio的源码如下, 播放音频的时候在浏览器和调试器的debug环境会报如上错误,但是不影响iPhone等手机的使用

<audio id="audio" src="http://awp.qq.com/act/a20160315live/shake_sound_male.mp3">
</audio>

function playOrPaused() {
console.log(typeof audio);
console.log(typeof audio.paused);

if (audio.paused) {
audio.play(); //ERROR:Uncaught (in promise) DOMException: The element has no supported sources.
}
}
1
2
3
4
5
6
7
8
9
10
11
查阅相关资料发现audio可以支持两种方式设置src,如下:

1. Permitted content: If the element has a src attribute: zero or more <track> elements, followed by transparent content that contains no media elements — that is, no <audio> or <video> elements.

2. Else: zero or more <source> elements, followed by zero or more <track> elements, followed by transparent content that contains no media elements, that is no <audio> or <video> elements.
1
2
3
并且:src嵌入的音频的URL。 该URL应遵从 HTTP access controls. 这是一个可选属性;你可以在audio元素中使用 元素来替代该属性指定嵌入的音频。

于是改成第二种方案,解决问题,如下:

<audio id="audio">
<source src="http://ossweb-img.qq.com/images/lol/m/act/a20160315live/shake_sound_male.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
1
2
3
4
参考资料:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio
https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/audio
http://www.w3schools.com/html/html5_audio.asp

4. 部分安卓机(如vivo)在微信浏览器里面audio播放不了、没有声音,但是在手机自带的浏览器没问题
解决方案:

```
document.addEventListener('WeixinJSBridgeReady', function () {

audio = document.getElementById("audio");

if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
alert('本设备不支持devicemotion事件');
return ;
}

shakeOn(1000);//摇一摇动画示例
});

```

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
5. WARNING: Deferred long-running timer task(s) to improve scrolling smoothness. See crbug.com/574343.
setTimeout(function () {
shakeOff();
}, n);
1
2
3
在原关闭摇一摇动画效果中,发现有时候debug调试器反馈如山WARNING,通过查资料发现:

The warning is telling you that your timer wasn’t fired on time because it is a long running callback (> 50ms) and the user was/is about to scroll. While your callback is running, Chrome can’t start scrolling the page so this results in “jank”, user input not being handled in a timely manner. To make the experience better for the user, Chrome decided to postpone firing that callback until a time where running it won’t negatively affect the user.

I don’t know the details of what you’re trying to do, but the correct way to do things would be to chunk up your one big callback into smaller batches and spread them out so that any one call will not noticeably delay user actions. You could additionally look at using requestIdleCallback which will call your function only when Chrome is idle and is ideally suited for non-time critical tasks. (However, requestIdleCallback is supported only on Chrome as of now).
————————————————
版权声明:本文为CSDN博主「jason09527」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhuizhuziwo/article/details/51852799

标签:acceleration,info,audio,last,音频,HTML5,var,摇一摇
From: https://www.cnblogs.com/jmbt/p/16608628.html

相关文章

  • 如何給html5的video的src地址不以明文方式顯示,以防止下載
    chrome:chrome的video標籤支持srcredirect。<videoid="video"src="{:U('/Portal/Video/token')}"autoplay="true"controlstype="video/mp4"class="videoplayer"po......
  • 开发那些事儿:EasyCVR接入华为设备,编辑开启音频不生效的问题修复
    EasyCVR平台是我们支持协议最全面的视频平台,它能支持主流协议包括国标GB/T28181、RTMP、RTSP/Onvif协议,以及厂家的私有协议,如海康Ehome、海康SDK、大华SDK等。平台可拓展性......
  • webrtc 渲染音频时遇到的问题
    有用户反馈连麦时,直播间会有电流声,后面排查发现是webrtc内部播放器渲染音频时,用户的播放设备不支持48000hz采样率(我们传输的音频采样率都是48000hz),导致音频数据受损而......
  • HTML5中datalist的用法
    <datalist>控件可以为输入框提供一些备选选项,当用户输入的内容与备选选项文字相同时,将会显示智能感应注:此时的option为单标签<inputtype='text'list='province-list'......
  • HTML4与HTML5中label标签的不同用法
    label标签:用来将文字和单选或者多选按钮进行绑定。 html4中label标签需要通过for和id配合使用:<inputtype='radio'id='nan'/><labelfor='nan'>男</label> html......
  • 前端 | HTML5基础知识
    1HTML定义HTML(英文HyperTextMarkupLanguage的缩写)中文译为“超文本标签语言”,主要是通过HTML标签对网页中的文本、图片、声音等内容进行描述。<strong>加粗字体</s......
  • 利用ffmpeg合并音频和视频
    一、当视频文件中没有音频时将audioname音频与videoname视频替换ffmpeg-ivideoname.mp4-imusic.mp3-c:vcopy-c:aaac-strictexperimentaloutputname.mp4二、当......