2011-11-28 56 views
3

试图通过JavaScript播放声音,并希望使用的sessionStorage如何动态创建一个JavaScript的sessionStorage内的阵列/ HTML5

下面是一个简化版本,在Android设备/ FF的Linux播放声音/秒动态地改变它/ Win当你点击“sprite me”按钮时 - 我已经包含了其他按钮来设置和检索HTML5中的会话值。

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

维基下方有Android手机的规格,其中它的工作原理:(三星Galaxy SII)的情况下,你有兴趣

http://globability.org/wiki/doku.php?id=current_working_specs_p-tab /又见http://globability.org/wiki/doku.php?id=mobile_pointing_tablet,以获取有关它是什么,一个正确的想法我正在努力。

我需要的是“play soundsprite”javascript,您可以在下面的部分中从sessionstorage加载并从插入到数组中插入从sessionstorage加载的值。

我不在寻找任何改变是如何播放声音 - 只需要在特定的javascript内部动态构建数组。

下面的代码基于www.phpied.com/audio-sprites/上的Stoyan Stefanov的声音想法 - 用于减少播放声音所需的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原型:)

回答

1

啊...这是SOOOOO简单,我是如此接近解决它自己,但在freelancer.com一种自由职业者谁也perviously帮助我得到我的想法来工作再次做到这一次一个......

而且事不宜迟:

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

这就是我需要的东西 - 我一直在尝试做相同的,但没有前面的’EVAL”和paranthesis围绕论点....

哦,不要忘记通过点击该按钮尝试还是会有没有声音出来扬声器的前设置的值;)

这里有一个链接到工作页面,如果你想尝试,如果你想看到它的代码是“entirity”:http://globability.org/webapp/asprite20111201_2.html - 感谢那些你投票的q提问!!!

+0

***如果您觉得它有帮助,请将问题投票!*** –

+0

http://globability.org/webapp/asprite20111205_5。html - 一个更好的例子,其中setanyvalue(arg1,arg2)函数将任意值传递给sessionstorage,并自动开始播放声音sprite(用于未来版本,使用从数据库读取的值等) –

+0

检查http://globability.org /wiki/doku.php?id=mobile_pointing_tablet并查看原型部分以了解最新的开发情况 –