admin管理员组文章数量:1384850
So I'm having a little trouble with videos, i have a website and there i have the same video displayed in 3 different pages. The videos are all paused until the user clicks on them to start. The problem is when, i leave a page and the video is just there even if i have clicked on play or pause or haven't done anything at all, the other two, give me an error saying vid.pause is not a function.
This is the HTML part ->
<video id="{{vid?.id}}" src={{vid?.video}} onloadedmetadata="this.paused = true; this.currentTime = 1" (click)="playPause(vid)"> </video>
And the js ->
playPause(vid) {
var vid = (<HTMLMediaElement>document.getElementById(vid.id));
if (vid.paused == true) {
vid.play();
} else {
vid.pause();
vid.currentTime = 1;
}
}
So I'm having a little trouble with videos, i have a website and there i have the same video displayed in 3 different pages. The videos are all paused until the user clicks on them to start. The problem is when, i leave a page and the video is just there even if i have clicked on play or pause or haven't done anything at all, the other two, give me an error saying vid.pause is not a function.
This is the HTML part ->
<video id="{{vid?.id}}" src={{vid?.video}} onloadedmetadata="this.paused = true; this.currentTime = 1" (click)="playPause(vid)"> </video>
And the js ->
playPause(vid) {
var vid = (<HTMLMediaElement>document.getElementById(vid.id));
if (vid.paused == true) {
vid.play();
} else {
vid.pause();
vid.currentTime = 1;
}
}
Share
Improve this question
edited Nov 2, 2018 at 19:48
Luis Pinho
asked Nov 2, 2018 at 19:08
Luis PinhoLuis Pinho
551 gold badge1 silver badge10 bronze badges
2 Answers
Reset to default 2The problem for me was that I tried pausing a jQuery object, but that's not possible. You need to pause a DOM element. So I corrected it by converting my jQuery object to a DOM element.
BEFORE:
$("#video").pause();
AFTER / SOLVED:
$("#video")[0].pause();
UPDATE:
One thing to notice is:
You are passing the vid
property to the element and then back to the .ts
. This is not good practices.
Also, you are using variables inside properties the wrong way.
The best way to do this would be:
.html file
<video #myVideo [id]="vid?.id" [src]="vid?.video" onloadedmetadata="this.paused = true; this.currentTime = 1" (click)="playPause(myVideo)"> </video>
.ts file
playPause(vid: HTMLMediaElement) {
if (vid.paused == true) {
vid.play();
} else {
vid.pause();
vid.currentTime = 1;
}
}
My guess is you have a difference in your HTML and JS files.
In your HTML file, you have the video id as vid.id
.
In your JS file, you are using getElementById
, passing the id as vid.video
.
Therefore var vid
will be undefined
and vid.pause()
won't be a function.
You probably have to use var vid = (<HTMLMediaElement>document.getElementById(vid.id));
本文标签: typescriptJavascript video pause is not a functionStack Overflow
版权声明:本文标题:typescript - Javascript video pause is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744538911a2611486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论