将背景音乐放入Angular 6

时间:2018-07-03 16:05:53

标签: angular

我正在尝试在Angular 6项目中使用免费音乐,但我无法使其正常工作。 我试过使用咆哮,什么也没做,这是我所做的:

import { Component, OnInit } from '@angular/core';
import {Howl, Howler} from 'howler';

@Component({
  selector: 'app-audio',
  templateUrl: './audio.component.html',
  styleUrls: ['./audio.component.css']
})
export class AudioComponent implements OnInit {

  constructor() {
  }

  ngOnInit() {
    const sound = new Howl({
      src: ['sound.webm', 'sound.mp3']
    });

    // Play the sound.
    sound.play();

    // Change global volume.
    Howler.volume(0.5);
  }

}

2 个答案:

答案 0 :(得分:2)

如果找不到声音文件,可能是路径问题,但是我看到您也可以回退到html5。

ref='_scrollView'

还要确保您已安装类型?

var sound = new Howl({ src: ['assets/path-to-audio/001.mp3'], html5 :true }); 
sound.play()

答案 1 :(得分:1)

查看他们网站上的其他示例,如果在ngOnInit之外实例化声音变量该怎么办?

import { Component, OnInit } from '@angular/core';
import {Howl, Howler} from 'howler';

@Component({
  selector: 'app-audio',
  templateUrl: './audio.component.html',
  styleUrls: ['./audio.component.css']
})
export class AudioComponent implements OnInit {

  sound = new Howl({
     src: ['sound.webm', 'sound.mp3']
  });

  constructor() {
  }

  ngOnInit() {
    // Play the sound.
    this.sound.play();

    // Change global volume.
    Howler.volume(0.5);
  }

}