简体   繁体   English

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

[英]How to dynamically create an array within a javascript sessionstorage / HTML5

Trying to play a sound via javascript and want to change it dynamically using sessionstorage 尝试通过javascript播放声音,并希望使用sessionstorage动态更改它

The following is a simplified version that plays sound/s in android/FF Linux/Win when u click the "sprite me button" - I've included other buttons for the example to set and retrieve session values in HTML5. 以下是在Android / FF Linux / Win中播放声音的简化版本,当您单击“sprite me”按钮时 - 我已在示例中包含其他按钮以在HTML5中设置和检索会话值。

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

The wiki below has Android phone specs where it works: (Samsung Galaxy SII) in case you're interested 下面的wiki有Android手机规格,如果您感兴趣,可以使用(三星Galaxy SII)

http://globability.org/wiki/doku.php?id=current_working_specs_p-tab / also see http://globability.org/wiki/doku.php?id=mobile_pointing_tablet to get a proper idea about what it is that I am working on. http://globability.org/wiki/doku.php?id=current_working_specs_p-tab /另见http://globability.org/wiki/doku.php?id=mobile_pointing_tablet ,以便对我的内容有所了解我正在努力。

What I need is to have the "play soundsprite" javascript that you can see below in the following section load from sessionstorage and insert values loaded from sessionstorage inserted into an array. 我需要的是使用“play soundsprite”javascript,你可以在下面的部分中看到,从sessionstorage加载并插入从插入到数组中的sessionstorage加载的值。

I am not looking for any changes is how the sound is played back - just need to make a dynamically built array work from within the particular javascript. 我不是在寻找声音如何播放的任何变化 - 只需要在特定的javascript中制作动态构建的数组。

The code below is based on the soundsprite idea from www.phpied.com/audio-sprites/ by Stoyan Stefanov - made to reduce the http calls needed for playing sounds... Also stabilizes the sound quality, less choppy sound etc. 下面的代码基于来自www.phpied.com/audio-sprites/的Storman Stefanov的soundsprite想法 - 用于减少播放声音所需的http调用...同时稳定音质,减少断电等。

Antway here goes: YOU ONLY NEED TO LOOK AT PSEUDOCODE SECTION - The rest is functioning Antway在这里:你只需要看看PSEUDOCODE部分 - 其余部分正在运作

<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>

Any ideas on how to dynamically create the 'wordgen' array within the javascript based on the values is sessionstorage ? 关于如何根据值在javascript中动态创建'wordgen'数组的任何想法都是sessionstorage?

The whole code/working example can be seen here: http://globability.org/webapp/asprite20111124_8.html 整个代码/工作示例可以在这里看到: http//globability.org/webapp/asprite20111124_8.html

I know the page is one ugly mess... but this is an alpha prototype :) 我知道页面是一个丑陋的混乱......但这是一个alpha原型:)

Argh... it was sooooo simple, I was this close to solving it myself, however a kind freelancer at freelancer.com who has perviously helped me getting one of my ideas to work did it again this time... 哎呀...这太简单了,我自己也很接近解决这个问题,但是freelancer.com上的一位善意的自由职业者已经帮助我实现了我的一个想法,这次又做了...

And without further ado: 而且没有进一步的麻烦:

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

That was what I needed - I had been trying to do the same but without the " eval " in front and the paranthesis around the arguments.... 这就是我所需要的 - 我一直试图做同样的事情但没有前面的“ 评估 ”和围绕论点的禁忌 ......

Oh and don't forget to set the value by clicking the button before you try or there will be no sound coming out of your speakers ;) 哦,不要忘记在尝试之前单击按钮设置值,否则扬声器没有声音;)

Here's a link to the working page if you care to try and if you want to see the code in it's "entirity": http://globability.org/webapp/asprite20111201_2.html - Thanks to those of you voting the question up!!! 这是工作页面的链接,如果你想尝试,如果你想看到它的“entirity”中的代码: http ://globability.org/webapp/asprite20111201_2.html - 感谢你们那些投票的问题! !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM