如何使用 Angular/Typescript 获得波形音乐播放器
How to get a Waveform Music Player with Angular/Typescript
我的目标是在我的 angular 8 项目中设置一个简单的音乐播放器,以静态波形显示插入的音乐,类似于 soundclouds 播放器:
根据我的搜索,这可以通过以下任一方式完成:
峰 JS: https://medium.com/better-programming/peaks-js-interact-with-audio-waveforms-b7cb5bd3939a
振幅 JS : https://521dimensions.com/open-source/amplitudejs/docs/configuration/
或 Wavesurfer JS:https://wavesurfer-js.org/
不幸的是,none 有一个不错的文档,说明如何将其正确导入 angular 或一般的打字稿。
我用 Wavesurfer JS 尝试过但失败了。
component.ts :
import { WaveSurfer } from 'wavesurfer.js';
export class MusicComponent {
public wavesurfer
constructor() {
this.wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple'
});
this.wavesurfer.load('./assets/img/hollywood.wav');
}
}
html
<div id="waveform"></div>
现在的问题是:
1. 关于如何使用 angular 实现这样的播放器,还有其他一些我没有找到的好的做法吗?
2. 有没有办法让 wavesurf 在 Angular 中工作,还是我的代码有误。
是的,有可能,我做到了。
检查下面的代码 (.ts):
import {Component, OnInit, ViewChild, ElementRef, AfterViewInit, ChangeDetectorRef} from '@angular/core';
import WaveSurfer from 'wavesurfer.js';
import TimelinePlugin from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.min.js';
import Regions from 'wavesurfer.js/dist/plugin/wavesurfer.regions.min.js';
@Component({
selector: 'app-graph',
templateUrl: './graph.component.html',
styleUrls: ['./graph.component.css']
})
export class GraphComponent implements OnInit, AfterViewInit {
wave: WaveSurfer = null;
url = '../../assets/sinus.wav';
public graph = undefined;
constructor( private cdr: ChangeDetectorRef) {
}
ngOnInit(): void {
}
onPreviewPressed(): void {
if (!this.wave) {
this.generateWaveform();
}
this.cdr.detectChanges();
Promise.resolve().then(() => this.wave.load(this.url));
}
onStopPressed(): void {
this.wave.stop();
}
generateWaveform(): void {
Promise.resolve(null).then(() => {
this.wave = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple',
plugins: [
TimelinePlugin.create({
container: '#wave-timeline'
}),
Regions.create({
regions: [
{
start: 1,
end: 3,
loop: false,
color: 'hsla(400, 100%, 30%, 0.5)'
}, {
start: 5,
end: 7,
loop: false,
color: 'hsla(200, 50%, 70%, 0.4)'
}
],
dragSelection: {
slop: 5
}
})
]
});
this.wave.on('ready', () => {
alert('I\'m ready');
this.wave.play();
});
});
}
}
在 HTML 文件上:
<div id="waveform"></div>
<div id="wave-timeline"></div>
<div *ngIf="wave" class="controls">
<button (click)="onStopPressed()">Stop</button>
</div>
<button (click)="onPreviewPressed()">Click me</button>
我的目标是在我的 angular 8 项目中设置一个简单的音乐播放器,以静态波形显示插入的音乐,类似于 soundclouds 播放器:
根据我的搜索,这可以通过以下任一方式完成:
峰 JS: https://medium.com/better-programming/peaks-js-interact-with-audio-waveforms-b7cb5bd3939a
振幅 JS : https://521dimensions.com/open-source/amplitudejs/docs/configuration/
或 Wavesurfer JS:https://wavesurfer-js.org/
不幸的是,none 有一个不错的文档,说明如何将其正确导入 angular 或一般的打字稿。
我用 Wavesurfer JS 尝试过但失败了。
component.ts :
import { WaveSurfer } from 'wavesurfer.js';
export class MusicComponent {
public wavesurfer
constructor() {
this.wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple'
});
this.wavesurfer.load('./assets/img/hollywood.wav');
}
}
html
<div id="waveform"></div>
现在的问题是:
1. 关于如何使用 angular 实现这样的播放器,还有其他一些我没有找到的好的做法吗?
2. 有没有办法让 wavesurf 在 Angular 中工作,还是我的代码有误。
是的,有可能,我做到了。 检查下面的代码 (.ts):
import {Component, OnInit, ViewChild, ElementRef, AfterViewInit, ChangeDetectorRef} from '@angular/core';
import WaveSurfer from 'wavesurfer.js';
import TimelinePlugin from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.min.js';
import Regions from 'wavesurfer.js/dist/plugin/wavesurfer.regions.min.js';
@Component({
selector: 'app-graph',
templateUrl: './graph.component.html',
styleUrls: ['./graph.component.css']
})
export class GraphComponent implements OnInit, AfterViewInit {
wave: WaveSurfer = null;
url = '../../assets/sinus.wav';
public graph = undefined;
constructor( private cdr: ChangeDetectorRef) {
}
ngOnInit(): void {
}
onPreviewPressed(): void {
if (!this.wave) {
this.generateWaveform();
}
this.cdr.detectChanges();
Promise.resolve().then(() => this.wave.load(this.url));
}
onStopPressed(): void {
this.wave.stop();
}
generateWaveform(): void {
Promise.resolve(null).then(() => {
this.wave = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple',
plugins: [
TimelinePlugin.create({
container: '#wave-timeline'
}),
Regions.create({
regions: [
{
start: 1,
end: 3,
loop: false,
color: 'hsla(400, 100%, 30%, 0.5)'
}, {
start: 5,
end: 7,
loop: false,
color: 'hsla(200, 50%, 70%, 0.4)'
}
],
dragSelection: {
slop: 5
}
})
]
});
this.wave.on('ready', () => {
alert('I\'m ready');
this.wave.play();
});
});
}
}
在 HTML 文件上:
<div id="waveform"></div>
<div id="wave-timeline"></div>
<div *ngIf="wave" class="controls">
<button (click)="onStopPressed()">Stop</button>
</div>
<button (click)="onPreviewPressed()">Click me</button>