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

前言感想:不放过任何一个WARNING、ERROR或者不够好的体验点,持续不断优化,精益求精,我们就能够得到提高。

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

}

于是开始研究起上面的代码不够灵敏的原因,发现:

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.z);

document.getElementById("moAccelGrav").innerHTML = info;

于是,优化后代码如下:

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;

}

}

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等手机的使用

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.

}

}

查阅相关资料发现audio可以支持两种方式设置src,如下:

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

2. Else: zero or more elements,followed by zero or more elements,followed by transparent content that contains no media elements,that is no or elements.

并且:src嵌入的音频的URL。 该URL应遵从 HTTP access controls. 这是一个可选属性;你可以在audio元素中使用 元素来替代该属性指定嵌入的音频。

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

Your browser does not support the audio tag.

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

```

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

audio = document.getElementById("audio");

if (window.DeviceMotionEvent) {

window.addEventListener('devicemotion',false);

} else {

alert('本设备不支持devicemotion事件');

return ;

}

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

});

```

5. WARNING: Deferred long-running timer task(s) to improve scrolling smoothness. See crbug.com/574343.

setTimeout(function () {

shakeOff();

},n);

在原关闭摇一摇动画效果中,发现有时候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).

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/456958.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

调用API发送短信python

import http.client import urllibhost "106.ihuyi.com" sms_send_uri "/webservice/sms.php?methodSubmit"# 用户名是登录用户中心->验证码短信->产品总览->APIID account "xxxxxxxx" # 密码 查看密码请登录用户中心->验证码短…

JAVA内部类使用

一、什么是内部类? 一个类的定义放在另一个类的内部,这个类就叫做内部类 二、内部类有那些特性? 1、内部类仍然是一个独立的类,在编译之后内部类会被编译成独立的.class文件,但是前面冠以外部类的类名和$符号 。  2、…

初学大数据之模块集成:Pycharm安装numpy,scipy,sklearn等包时遇到的各种问题的一键解决方法

最近在学习机器学习,要用Python写程序,习惯了用IDE软件,所以就使用Pycharm软件。但是在导入类似numpy,sklearn等模块的时候,发现了各种问题(如Python版本与模块之间的兼容等各类问题),上网找了许多方法&…

html 圆环实现多种颜色,SVG实现多彩圆环倒计时效果的示例代码

圆环倒计时我们经常见到,实现的方法也有很多种。但是本文将介绍一种全新的实现方式,使用SVG来实现倒计时功能。本文主要用到了SVG的stroke-dasharray和stroke-dashoffset特性。下图是倒计时运行效果:SVG倒计时案例下面说说相关的实现代码。cs…

调用API发送邮件163邮箱Python

#发邮件的库 import smtplib# from email.mime.text import MIMEText #SMTP服务器 SMTPSever "smtp.163.com" #发邮件的地址 sender "18332191389163.com" #发送这邮箱的密码 passwd "xxxxxxxx"#设置发送的内容 message "liu wang is …

u-boot文件夹

参考网址: http://www.cnblogs.com/PengfeiSong/p/6392056.html http://www.360doc.com/content/14/1114/14/8890849_425067013.shtml 转载于:https://www.cnblogs.com/lijimmy/p/6580870.html

初学大数据之Python中5个最佳的数据科学库的学习

在下载了pycharm软件以及通过前两篇文章,配置了相应的模块包之后,那就开始对常用的模块的学习,以便后期利用这些模块对数据做模型化处理。 如果你已经决定把Python作为你的编程语言,那么,你脑海中的下一个问题会是&…

配置mq

mq的实现可以是apache的&#xff0c;也可以是ibm的&#xff0c;配置不同的地方是connectionFactory和queue和topic应用的包不同 <!-- 配置链接器&#xff0c;注入apache的实现 --><bean id"connectionFactory"class"org.springframework.jms.connectio…

模拟银行自动提款系统python

列出对象及属性名称行为...py 人 类名&#xff1a;Person 属性&#xff1a;姓名 身份证号 电话 卡 行为&#xff1a;卡 类名&#xff1a;Card 属性&#xff1a;卡号 密码 余额 行为&#xff1a;银行 类名&#xff1a;Bank 属性&#xff1a;用户列表 提款机提款机 类名&#xf…

帮助文件html打不开,chm帮助文件打不开全是代码?这几种解决方法了解一下

win10系统chm帮助文件打不开怎么办?近期使用win10 版本 1809 (OS 内部版本 17763.864)系统的用户反应电脑打不开这个chm帮助文件的情况&#xff0c;打开后显示不正常&#xff0c;针对这样的问题如何解决呢?针对chm文件打开异常的现象大家可以参考本文中飞飞系统介绍的方法来修…

关于四种语言中substring()方法参数值的解析

1.关于substring(a,b)Jsvar str"bdqn";var resultstr.substring(1,2);alert(result);第一个参数&#xff1a;开始的位置&#xff0c;从0开始数第二个参数&#xff0c;结束的索引&#xff0c;从1开始数&#xff0c;而不是获取几个长度SQLselect substring(bdqn,2,1)第…

python中tkinter的使用-上

00基础代码 import tkinterwin tkinter.Tk() win.title("Liuwang") win.geometry("400x40020020")win.mainloop() 01简单示例 #创建主窗口 win tkinter.Tk() #设置标题 win.title("Liuwang") #设置大小和位置 win.geometry("400x40020…

滚动条样式修改

/*滚动条*/ ::-webkit-scrollbar { width: 4px; height: 4px; background-color: #F5F5F5; } /*定义滚动条轨道 内阴影圆角*/ ::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.…

sklearn中常用的数据预处理方法

常见的数据预处理方法&#xff0c;以下通过sklearn的preprocessing模块来介绍; 1. 标准化&#xff08;Standardization or Mean Removal and Variance Scaling) 变换后各维特征有0均值&#xff0c;单位方差。也叫z-score规范化&#xff08;零均值规范化&#xff09;。计算方式是…

儿童学计算机编程好处,儿童学习编程有什么好处

原标题&#xff1a;儿童学习编程有什么好处前几年中国家长可能对少儿编程教育感到陌生。但随着这两年美国STEM教育在中国的流行&#xff0c;以及今年国务院普及中小学阶段人工智能、编程教育规划的发布&#xff0c;现在国内也渐渐掀起少儿学习编程的风潮。孩子学电脑编程&#…

python中tkinter的使用-中

00Listbox控件 import tkinterwin tkinter.Tk() win.title("Liuwang") win.geometry("400x40020020") 列表框控件&#xff0c;可以包含一个或者多个文本框 作用&#xff1a;在listbox控件的小窗口显示一个字符串 #1、创建一个listbox,添加几个元素&#…

SharePoint Server 2016 PWA(Project web app) 被变为只读模式

今天有同事反应了一个状况&#xff0c;我们SharePoint 2016里面集成的Project Web App(以下简称PWA)变成 read-only 只读模式了&#xff01;今天就给大家分享一下我的排查过程&#xff0c;供大家参考。 整个过程我一共使用了五种办法&#xff0c;结果最后一种才生效&#xff0c…

HDU 5741 Helter Skelter(构造法)

【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid5741 【题目大意】 一个01相间的串&#xff0c;以0开头&#xff0c;给出的序列每个数字表示连续的0的个数或者1的个数&#xff0c;现在有m个询问&#xff0c;求0的个数为a且1的个数为b的串是否存在。 【题解】 我们发现…

集成学习之参数调整策略

1 Random Forest和Gradient Tree Boosting参数详解 在sklearn.ensemble库中&#xff0c;我们可以找到Random Forest分类和回归的实现&#xff1a;RandomForestClassifier和RandomForestRegression&#xff0c;Gradient Tree Boosting分类和回归的实现&#xff1a;GradientBoost…

python中tkinter的使用-下

00表格数据 import tkinter from tkinter import ttkwin tkinter.Tk() win.title("Liuwang") win.geometry("400x40020020")#表格 tree ttk.Treeview(win) tree.pack() #列 tree["columns"] ("姓名","年龄","身高&…