为什么 audio.pause() 不能使用 TypeScript,但是 audio.play();工作得很好吗?

why audio.pause() can not working with TypeScript, but audio.play(); works very well?

我对 HTMLMediaElement 的 pause() 函数有疑问。我试图暂停我的音频,但没有任何反应。 play() 每次都运行良好:

export class personalCard { 
j = 0;

playAudio(){
let aud = new Audio ('../../assets/aud/Magdalena_Su_ß.opus');


this.j= this.j + 1;

if(this.j % 2 == 0){
  aud.pause();
  console.log('Pause');
   }

else if(this.j % 2 != 0){
  aud.play();
  console.log('Play');
   }

 }
}

HTML:

<button id="volume" (click)="playAudio()"></button>

我还可以在控制台上看到日志 'pause',但是 aud.pause();永远无法工作。我不使用任何音频框架。 有人可以帮我解决问题吗?

非常感谢, 山姆

为了访问 pause()、mute() 或 load() 变量,必须在 class 中声明 aud 以进行全局访问。

解决方案:

export class personalCard { 
j = 0;
aud = new Audio ('../../assets/aud/Magdalena_Su_ß.opus');

playAudio(){

this.j= this.j + 1;

if(this.j % 2 == 0){
  aud.pause();
  console.log('Pause');
  }

else if(this.j % 2 != 0){
  aud.play();
  console.log('Play');
     }
   }

}