【问题标题】:Tone JS - Transport.stop(); does not work with scheduled eventsTone JS - Transport.stop();不适用于预定事件
【发布时间】:2021-01-08 22:24:47
【问题描述】:

我正在将 Tone JS 用于一个项目,并且我正在使用 Transport.scheduleOnce 通过 Sampler 安排事件。这是我到目前为止所拥有的,也是其中的fiddle(您可能需要单击运行几次才能听到最初加载小提琴时的音频)

我的代码:

const sound = 'https://archive.org/download/testmp3testfile/mpthreetest.mp3';
let samplerBuffer;

const sampler = new Promise((resolve, reject) => {
   samplerBuffer = new Tone.Sampler(
    { 
      A1: sound
    },
    {
      onload: () => {
        resolve()
      }}
  ).toMaster();
})


sampler.then(() => {
  Tone.Transport.scheduleOnce(() => {
    samplerBuffer.triggerAttack(`A1`, `0:0`)
  });

  Tone.Transport.start();
  
  setTimeout(() => {
    console.log('Now should be stopping');
    Tone.Transport.stop();
  },1000)
})

我正在尝试使用Transport.stop() 方法在 1 秒后停止播放音频,但它似乎不起作用。我想我已经按照我应该的方式遵循了文档,所以我哪里出错了?

【问题讨论】:

    标签: javascript web-audio-api tone.js


    【解决方案1】:

    Tone.Transport 正在触发您的样本。
    如果您只想播放“点唱机”之类的声音,您可以使用Tone.Player

    如果你真的需要一个采样器,那么应​​该查看Envelopes,因为采样器使用一个。

    简而言之:Tone.Transport 就像音乐会中的大师。 Transport 只是设置时间(只有 BPM 不是播放速度)。 Tone.Transport.start() 将触发所有已注册的乐器(在您的情况下为采样器)开始执行您对它们进行的任何编程操作。如果您想停止采样器在此模式下播放。你可以samplerBuffer.releaseAll()

    const sound = 'https://archive.org/download/testmp3testfile/mpthreetest.mp3';
    let samplerBuffer;
    
    const sampler = new Promise((resolve, reject) => {
       samplerBuffer = new Tone.Sampler(
        { 
          A1: sound
        },
        {
          onload: () => {
            resolve()
          }}
      ).toMaster();
    })
    
    
    sampler.then(() => {
      Tone.Transport.scheduleOnce(() => {
        samplerBuffer.triggerAttack(`A1`, `0:0`)
      });
    
      Tone.Transport.start();
      setTimeout(function() {
        console.log('Now should be stopping');
        samplerBuffer.releaseAll();
        // samplerBuffer.disconnect();
      },1000)
    })
    

    https://jsfiddle.net/9zns7jym/6/

    【讨论】:

      猜你喜欢
      • 2021-01-20
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 2020-10-01
      • 2012-06-24
      • 2015-03-31
      • 1970-01-01
      • 2012-03-17
      相关资源
      最近更新 更多