我想在我的Angular项目上运行WaveSurfer.js,它无法按我的意愿运行。
WaveSurfer在Index中可以完美工作。但是,我希望在组件中使用WaveSurfer,但它不起作用。

指标(工作):

<!doctype html>
<html lang="en">
<head>
<script src="assets/js/wavesurfer.min.js"></script><
<script src="wavesurfer.microphone.min.js"></script>

<meta charset="utf-8">
<title>WaveAngular</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<div id="waveform"></div>
<script src="assets/js/wavesurfer.js">
</script>
</script>
</body>
</html>


组件(无效):

<div class="dashboard-voice-parent">
<div class="dashboard-voice-Visualication">
<div id="waveform"></div>
<script src="assets/js/wavesurfer.js"></script>

 </div>
 <div class="dashboard-voice-buttoncontainer">
  <div class="dashboard-voice-buttonborder ">
 <button type="button" class="dashboard-voice-animated"
  onclick="microphone.pause()" >
      <img src="/assets/svg/PauseBTN.png" >
    </button>
    <button type="button" class="dashboard-voice-VoiceBTN"
    onclick="microphone.start()" >
      <img src="/asset`enter code here`s/svg/VoiceBTN.png">
    </button>
    <button type="button" class="dashboard-voice-animated"
    onclick="microphone.stopDevice()" >
      <img src="/assets/svg/StopBTN.png" >
    </button>
    </div>
   </div>
   </div>


错误:

Uncaught ReferenceError: microphone is not defined
at HTMLButtonElement.onclick ((index):17)


这是我在waveurfer.js中的JavaScript代码

  var
  wavesurfer=WaveSurfer.create({container:'#waveform',waveColor:'grey'});
  var microphone = Object.create(WaveSurfer.Microphone);

  microphone.init({
  wavesurfer: wavesurfer
  });

  microphone.on('deviceReady', function(stream) {
  console.log('Device ready!', stream);
  });
  microphone.on('deviceError', function(code) {
  console.warn('Device error: ' + code);
  });
  microphone.pause();
  microphone.play();
  microphone.stopDevice();


这是我正在使用的WaveSurfer:WaveSurfer.Microphone Example

我希望你们能在这个问题上帮助我!

最佳答案

根据wavesurfer.js Overview,您可以尝试从npm安装waveurfer。

npm install --save [email protected]

根据此issue on the wavesurfer github page,在将ES6导入与webpack正确配合使用时可能会有一些问题(Angular 4在幕后使用)。该问题详细介绍了如何将Wavesurfer及其插件导入到组件中。

组件中的import语句如下所示:

import WaveSurfer from 'wavesurfer.js';
import Microphone from 'wavesurfer.js/dist/plugin/wavesurfer.microphone.min.js';


我建议您尝试这种方法,并摆脱HTML中Wavesurfer的任何<script>标记引用。比较干净。

关于javascript - Angular 4无法在我的组件中运行WaveSurfer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45053909/

10-11 11:03