搖一搖 聲音 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.pswp.cn/news/456958.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/456958.shtml
英文地址,請注明出處:http://en.pswp.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"] ("姓名","年齡","身高&…