admin管理员组文章数量:1404938
I have created a simple Audio Player in React js with Typescript and I am trying to use the normal HTML5 functions to Play and Pause the Audio but it is not working.
When I do mytrack.play();
I get this error: Property 'play' does not exist on type 'HTMLElement'.
Here is my code:
import React, { Component } from "react";
import "./SimplePlayer.css";
import styled from "styled-ponents";
export interface SimplePlayerProps {}
var mytrack = document.getElementById("mytrack");
console.log(mytrack);
mytrack.play();
export class SimplePlayer extends Component<SimplePlayerProps> {
constructor(props: SimplePlayerProps) {
super(props);
console.log(this.props);
this.state = {};
}
render() {
return (
<div id="wrapper">
<audio id="mytrack" controls>
<source src="audio.mp3" />
</audio>
<nav>
<div id="defaultBar">
<div id="progressBar" />
</div>
<div id="buttons">
<button type="button" id="playButton" />
<button type="button" id="muteButton" />
<span id="currentTime">0:00</span>/
<span id="fullDuration">0:00</span>
</div>
</nav>
</div>
);
}
}
I have created a simple Audio Player in React js with Typescript and I am trying to use the normal HTML5 functions to Play and Pause the Audio but it is not working.
When I do mytrack.play();
I get this error: Property 'play' does not exist on type 'HTMLElement'.
Here is my code:
import React, { Component } from "react";
import "./SimplePlayer.css";
import styled from "styled-ponents";
export interface SimplePlayerProps {}
var mytrack = document.getElementById("mytrack");
console.log(mytrack);
mytrack.play();
export class SimplePlayer extends Component<SimplePlayerProps> {
constructor(props: SimplePlayerProps) {
super(props);
console.log(this.props);
this.state = {};
}
render() {
return (
<div id="wrapper">
<audio id="mytrack" controls>
<source src="audio.mp3" />
</audio>
<nav>
<div id="defaultBar">
<div id="progressBar" />
</div>
<div id="buttons">
<button type="button" id="playButton" />
<button type="button" id="muteButton" />
<span id="currentTime">0:00</span>/
<span id="fullDuration">0:00</span>
</div>
</nav>
</div>
);
}
}
Any Help would be really appreciated :) Many Thanks
Best Regards, Musayyab Naveed
Share Improve this question asked Mar 20, 2019 at 21:02 Musayyab NaveedMusayyab Naveed 3656 silver badges15 bronze badges2 Answers
Reset to default 5There are a couple of issues here.
- The
play
method exists onHTMLAudioElement
and notHTMLElement
.getElementById
returnsHTMLElement | null
, so you'd have to castmytrack
asHTMLAudioElement
for TypeScript to allow it.
But that's not the main problem.
- The real issue is that your code to get and play the audio file is running before the code (in
render
) that creates the audio element. If you're wanting to play the audio file immediately, try including theautoplay
property on theaudio
element.
//file for export
export const AudioPlay = (file) => {
new Audio(`/static/sounds/${file}`).play(); //under folder public
};
//file import
import {AudioPlay} from './m';
//onClick event
AudioPlay('stockin-ok.mp3');
本文标签:
版权声明:本文标题:javascript - Cannot Play Audio in React Typescript with play() function | Error: Property 'play' does not exist 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744871470a2629653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论