javascript - 如何在 javascript sessionstorage/HTML5 中动态创建数组

标签 javascript android arrays html audio

尝试通过 javascript 播放声音并希望使用 session 存储动态更改它

以下是一个简化版本,当您单击“sprite me 按钮”时,它会在 android/FF Linux/Win 中播放声音 - 我已经为示例包含了其他按钮以在 HTML5 中设置和检索 session 值。

http://globability.org/webapp/asprite20111124_8.html

下面的 wiki 有适用的 Android 手机规范:(Samsung Galaxy SII) 如果您有兴趣

http://globability.org/wiki/doku.php?id=current_working_specs_p-tab/另见 http://globability.org/wiki/doku.php?id=mobile_pointing_tablet以正确了解我正在从事的工作。

我需要的是让您在下一节中看到的“播放声音 Sprite ”javascript 从 sessionstorage 加载并将从 sessionstorage 加载的值插入到数组中。

我不希望改变声音的播放方式 - 只需要在特定的 javascript 中创建一个动态构建的数组即可。

下面的代码基于 Stoyan Stefanov 的 www.phpied.com/audio-sprites/的 soundsprites 想法 - 旨在减少播放声音所需的 http 调用...还可以稳定音质,减少声音的断断续续等.

Antway 在这里:您只需要查看伪代码部分 - 其余的都在运行

<script>
var thing = 'the thing';

function shut() {
if (typeof thing.pause !== 'undefined') {
    thing.pause();
}
}

function log(what) {
document.getElementById('log').innerHTML += what + "<br>";
}

var spriteme = function(){
var sprites = {
// id: [start, length]

'blank':[0.1, 0.5], //This the first sprite, it has to be the first defined and is 
called first, it is a blank piece of sound in the combined sound file and needed as of 
now.

'success':[13,  2,5],

/* I would like to be able to set the parameters i.e. sound bite to play dynamically - 
here a pseudocode example using session storage in preparation for getting the sound 
parameters from a database


'wordgen'[null,null]; 
//this array should be dynamically built from values read from the two session storage keys not sure you need the new thing in HTML5

sessionStorage.globabilitykey1;
sessionStorage.globabilitykey2;

strkey1=globabilitykey1
strkey2=globabilitykey2

var gkey1=parsefloat(strkey1)
var gkey2=parsefloat(strkey2)

'wordgen':[gkey1,gkey2]


and then the idea is to replace the array success in the script with the "generated" 
array 'wordgen' to allow dynamic seting of sound to play back  */       

//the following are sound bites from the collection of soundsprites the site plays from

'word1': [0.5, 2,36], //one
'word2': [3.1,  3.0], //two
'word3': [7.0,  1.82], //three
'word4': [10.03, 2], //four ?

},
song = ['blank', 'success'], 
//here you're setting the playback sequence (this is where I would like to replace 'success' with 'wordgen'
current = 0,
id = song[current],
start = 0,
end = sprites[id][1],
int;

thing = document.getElementById('sprite');
thing.play();

log('file: ' + thing.currentSrc);
log(id + ': start: ' + sprites[id].join(', length: '));

// change
int = setInterval(function() {
if (thing.currentTime > end) {
thing.pause();
if (current === song.length - 1) {
clearInterval(int);
return;
}
current++;
id = song[current];
start = sprites[id][0];
end = start + sprites[id][1]
thing.currentTime = start;
thing.play();
log(id + ': start: ' + sprites[id].join(', length: '));
}
}, 10);   
};
</script>

关于如何根据值在 javascript 中动态创建“wordgen”数组的任何想法是 sessionstorage 吗?

完整的代码/工作示例可以在这里看到:http://globability.org/webapp/asprite20111124_8.html

我知道这个页面是一团糟...但这是一个 alpha 原型(prototype) :)

最佳答案

啊...这太简单了,我差一点就自己解决了,但是 freelancer.com 上一位好心的自由职业者一直帮助我实现我的一个想法,这次又做了一次...

事不宜迟:

'wordgen':eval("["+sessionStorage.globabilitykey1+","+sessionStorage.globabilitykey2+"]"),

这就是我所需要的——我一直在尝试做同样的事情,但没有前面的“eval”和参数周围的paranthesis....

哦,不要忘记在尝试之前通过单击按钮来设置值,否则您的扬声器将没有声音;)

如果您想尝试一下,如果您想查看“实体”中的代码,这里有一个工作页面的链接:http://globability.org/webapp/asprite20111201_2.html - 感谢你们中的那些人投票赞成这个问题!!!

关于javascript - 如何在 javascript sessionstorage/HTML5 中动态创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8299722/

相关文章:

javascript - 如何随机化 HTML 元素的颜色属性?

javascript - 异常请求 : I need help animating text

javascript - 如何通过调用ajax请求从PHP页面重定向

android - Android 上的 MouseArea 虚拟双击

java - Android/Java - 单击 ListView 中的图像

php - 带左连接的 SQL 查询结果和带 PHP 的结果数组

javascript - Rails 和 YouTube API : Issues loading iframe

java - 了解导致信号 11 (SIGSEGV)、代码 1 (SEGV_MAPERR) 几秒钟后出现错误的 Android 问题

java - 如何将二进制字符串转换为 int 数组?

javascript - 显示一周中某天的消息