[英]Ionic 3 cordova button click to stop audio
Hey guys i have a situation that there is tab and press event on a grid item. 嗨,我有一个情况,在网格项目上有选项卡和新闻事件。 when i tab it plays an audio 1 and when i press it plays an audio 2 now the problem is when i multiple tab or click audio starts over lapping.
当我按Tab播放音频1时,当我按它播放音频2时,问题是当我多个选项卡或单击音频开始重叠时。 i need to stop previous audio and play it again when tab or pressed therefore i overcome overlapping of audios.
我需要停止上一个音频并在按Tab或按下时再次播放,因此我克服了音频重叠的问题。
<ion-content >
<ion-col (tap)="p10_1()" (press) = "p10_1l()">
<ion-grid >
<ion-row >
<div id = "container">
<div class = "sections" id = "sec1" >
A
</div><!--
--><div class = "sections" id = "sec2" >
B
</div><!--
--><div class = "sections" id = "sec3" >
C
</div>
</div>
</ion-row>
</ion-grid>
this is html above 这是上面的html
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
bleep: Audio;
p10_1()
{
document.getElementById("sec1").style.color = "red";
document.getElementById("sec2").style.color = "red";
document.getElementById("sec3").style.color = "red"
if (this.bleep) {
this.bleep.stop().then(() => this.bleep =
this.play('./assets/sounds/q1p10_1.mp3', true));
} else {
this.bleep = this.play('./assets/sounds/q1p10_1.mp3', true);
}
}
p10_1l()
{
if (this.bleep) {
this.bleep.stop().then(() => this.bleep =
this.play('./assets/sounds/q1p10_1l.mp3', false));
} else {
this.bleep = this.play('./assets/sounds/q1p10_1l.mp3', false);
}
}
play(x:string, black:boolean): Audio {
let bleep = new Audio();
bleep.src = x;
bleep.play();
return bleep;
}
}
this is .ts 这是.ts
thanks in anticipation. 谢谢期待。
You should add your bleep var as controller attribute, so you can stop it before relaunching. 您应该将bleep var添加为控制器属性,以便可以在重新启动之前将其停止。 I have mutualized your code in play() function.
我已经在play()函数中使您的代码通用。 You may also use the uniqueId param but I don't know its behaviour.
您也可以使用uniqueId参数,但是我不知道它的行为。
export class YourComponent {
bleep: Audio;
p10_1()
{
document.getElementById("sec1").style.color = "red";
document.getElementById("sec2").style.color = "red";
document.getElementById("sec3").style.color = "red"
if (this.bleep) {
this.bleep.stop().then(() => this.bleep = this.play('./assets/sounds/q1p10_1.mp3', true));
} else {
this.bleep = this.play('./assets/sounds/q1p10_1.mp3', true);
}
}
p10_1l()
{
if (this.bleep) {
this.bleep.stop().then(() => this.bleep = this.play('./assets/sounds/q1p10_1l.mp3', false));
} else {
this.bleep = this.play('./assets/sounds/q1p10_1.mp3', false);
}
}
play(x:string, black:boolean): Audio {
let bleep = new Audio();
bleep.src = x;
bleep.play();
if (black) {
bleep.onended = function() {
document.getElementById("sec1").style.color = "black";
document.getElementById("sec2").style.color = "black";
document.getElementById("sec3").style.color = "black";
}
}
return bleep;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.