मैं एक खेल बना रहा हूं, जिसमें ध्वनि अक्सर खेला जाता है। मैंने देखा कि खेला जाने पर एक आवाज फिर से खेल नहीं पाएगी। उदाहरण के लिए, खिलाड़ी दीवार से टकराता है, एक "थंप" ध्वनि खेला जाता है। लेकिन, अगर खिलाड़ी एक दीवार से टकराता है, और फिर जल्दी से दूसरी दीवार के साथ टकराता है, तो केवल एक "थंप" ध्वनि खेला जाता है, और मेरा मानना है कि ऐसा इसलिए होता है क्योंकि पहली आवाज खत्म नहीं होती है। क्या यह सच है? मुझे इससे कैसे बचना चाहिए? मैं ध्वनि तीन बार पहले से लोड होने, हमेशा कि ध्वनि की एक अलग प्रतिलिपि खेलने के बारे में सोचा है, लेकिन इस बल्कि मूर्ख लगता है ...एचटीएमएल 5 में, क्या मैं एक ही समय में एक ही ध्वनि को एक से अधिक बार खेल सकता हूं?
हल:
बाहर कर देता है मैं सही है ... आप को पहले से लोड की जरूरत किया गया था ध्वनि के कई संस्करण और फिर गोलाकार रूप से उन्हें खेलते हैं।
कोड:
var ns = 3; //The number of sounds to preload. This depends on how often the sounds need to be played, but if too big it will probably cause lond loading times.
var sounds = []; //This will be a matrix of all the sounds
for (i = 0; i < ns; i ++) //We need to have ns different copies of each sound, hence:
sounds.push([]);
for (i = 0; i < soundSources.length; i ++)
for (j = 0; j < ns; j ++)
sounds[j].push(new Audio(sources[i])); //Assuming that you hold your sound sauces in a "sources" array, for example ["bla.wav", "smile.dog" "scream.wav"]
var playing = []; //This will be our play index, so we know which version has been played the last.
for (i = 0; i < soundSources.length; i ++)
playing[i] = 0;
playSound = function(id, vol) //id in the sounds[i] array., vol is a real number in the [0, 1] interval
{
if (vol <= 1 && vol >= 0)
sounds[playing[id]][id].volume = vol;
else
sounds[playing[id]][id].volume = 1;
sounds[playing[id]][id].play();
++ playing[id]; //Each time a sound is played, increment this so the next time that sound needs to be played, we play a different version of it,
if (playing[id] >= ns)
playing[id] = 0;
}
ऑडियो चलाने के लिए आप क्या उपयोग कर रहे हैं? मुझे लगता है कि यह '
'ध्वनि = नया ऑडियो (" souce.wav "); sound.play(); ' – corazza