Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

audio - Dynamically playing multiple sounds using javascript

    var actions = function(){return {whichPattern:"pattern1"}};
    var audio = new Audio();
    var _play = function(){
        var l = pattern[actions().whichPattern].instrument.length,
            times_played =0;
        var p = setInterval(function(){

var x = pattern[actions().whichPattern][times_played];
        for(var i=0; i<x.length;i++){
                var toBePlayed = pattern[actions().whichPattern][times_played][i],
                url= "All Drums sounds/"+toBePlayed+".wav";
                audio.src = url;
                audio.play();
                console.log(audio);
                times_played++;
        }

        }, pattern_config[actions().whichPattern].delay);   
    if(times_played === l){
        clearInterval(p);

    };

    var pp=document.getElementById("play_pause");
    pp.style.background="url('graphics/pause.png') no-repeat";
    pp.style.backgroundSize ="30px 28px"
    audio.source='All Drums sounds/'+pattern['pattern1'][1][0]+'.wav';
    audio.play();

    };

    var pattern_config = {
        pattern1:{
            WP_slotsCounter:0,
            instrument:["Kick_02", "F_T_03", "Rude_cymbal_02"],
                delay:10
        },
    };

    var pattern={
        pattern1: [], // [['asas', 'sf', 'asd'], ['svv','dgh','sdgh']]
    }

The above code is very simple. The first function return an object ...

well the main motive of mine is like :

assuming that pattern.pattern1 = [['asas', 'sf', 'asd'], ['svv','dgh','sdgh']] so i what that the all the strings in the pattern['pattern1'][0] should play at same instant and then with a delay of patter_config[actions().whichPattern].delay that sound files with that names of pattern['pattern1'][1] should play together.

so to achieve this i made the above function _play()

well the problem is that it is not playing the music files and not giving any errors too so that i can identify where is that problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

pattern["pattern1"] is an array of strings and does not have any 'instrument' property maybe you were trying to use pattern_config["pattern1"]

 var actions = function () {
     return {
         whichPattern: "pattern1"
     }
 };
 var audio = new Audio();
 var _play = function () {
     var l = pattern_config[actions().whichPattern].instrument.length,
         times_played = 0;
     var p = setInterval(function () {

         var x = pattern_config[actions().whichPattern][times_played];
         for (var i = 0; i < x.length; i++) {
             var toBePlayed = pattern_config[actions().whichPattern][times_played][i],
                 url = "All Drums sounds/" + toBePlayed + ".wav";
             audio.src = url;
             audio.play();
             console.log(audio);
             times_played++;
         }

     }, pattern_config[actions().whichPattern].delay);
     if (times_played === l) {
         clearInterval(p);

     };

     var pp = document.getElementById("play_pause");
     pp.style.background = "url('graphics/pause.png') no-repeat";
     pp.style.backgroundSize = "30px 28px"
     audio.source = 'All Drums sounds/' + pattern['pattern1'][1][0] + '.wav';
     audio.play();

 };

 var pattern_config = {
     pattern1: {
         WP_slotsCounter: 0,
         instrument: ["Kick_02", "F_T_03", "Rude_cymbal_02"],
         delay: 10
     },
 };

 var pattern = {
     pattern1: [['asas', 'sf', 'asd'], ['svv','dgh','sdgh']],
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...