angular8 - Angular 8,“AudioContext 不允许启动。它必须恢复”
问题描述
我正在构建一个监听服务的通知组件(角度 8),每次用户收到新通知时 - 一个 toastr 弹出并假设播放声音......我猜很简单 - 但我无法得到它工作。
我将 Howler 与 ngx-toastr 一起使用,我的第一种方法是手动触发将播放声音的元素 - 但我不断收到以下错误:
这是 Toastr 组件
import { Component, ElementRef, AfterViewInit, ViewChild } from "@angular/core";
import { Toast, ToastrService, ToastPackage } from "ngx-toastr";
import { Howl } from "howler";
@Component({
selector: "toast-component",
template: ` <div class="row toast-wrapper">
<div class="col-12">
<div class="toast-title" [attr.aria-label]="title">Hello User</div>
</div>
<div #audioDiv (click)="playSound()"></div>
</div>`,
})
export class ToastrTplComponent extends Toast implements AfterViewInit {
@ViewChild("audioDiv", { static: false }) audioDiv: ElementRef;
constructor(
protected toastrService: ToastrService,
public toastPackage: ToastPackage
) {
super(toastrService, toastPackage);
}
ngAfterViewInit() {
const event = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
});
this.audioDiv.nativeElement.dispatchEvent(event);
}
public playSound(msg: string) {
const sound = new Howl({
src: ["assets/audio/sound.wav"],
});
sound.play();
}
}
解决方案
推荐阅读
- twilio - 如何为 Twilio SMS StatusCallback URL 处理身份验证
- amazon-web-services - 在 ECR 中删除最新标签时使用什么图像
- javascript - 在带有特殊字符的参数列表后出现错误 Uncaught SyntaxError: missing )
- reactjs - 反应/选中复选框时如何通过条件
- java - 非法 base64 字符 7b
- typescript - 用于获取特定类型字段名称的类型
- kubernetes - 如何在不覆盖 mountPath 的情况下初始化配置文件
- google-apps-script - 在for循环中增加值的问题
- jenkins - Jenkins从服务在重启后仍然停止
- java - 使用 Spring AOP AspectJ Annotation 需要哪个 jar?