Cloning audio source without having to download it again

前端 未结 3 380
野趣味
野趣味 2020-12-06 19:02

I\'m creating a piano in the browser using javascript. In order for me to play the same key multiple times simultaneously, instead of just playing the Audio object, I clone

3条回答
  •  醉梦人生
    2020-12-06 19:36

    cloneNode have one boolean argument:

    var dupNode = node.cloneNode(deep);
    /*
      node
        The node to be cloned.
      dupNode
        The new node that will be a clone of node
      deep(Optional)
         true if the children of the node should also be cloned, or false to clone only the specified node.
    */
    

    Also note from MDN:

    Deep is an optional argument. If omitted, the method acts as if the value of deep was true, defaulting to using deep cloning as the default behavior. To create a shallow clone, deep must be set to false.

    This behavior has been changed in the latest spec, and if omitted, the method will act as if the value of deep was false. Though It's still optional, you should always provide the deep argument both for backward and forward compatibility

    So, try to use deep = false to prevent download resource:

    var audioSrc = new Audio('path/');
    window.onkeypress = function(event) {
        var currentAudioSrc = audioSrc.cloneNode(false);
        currentAudioSrc.play();
    }
    

提交回复
热议问题