There are a couple of issues here.

  1. The play method exists on HTMLAudioElement and not HTMLElement. getElementById returns HTMLElement | null, so you'd have to cast mytrack as HTMLAudioElement for TypeScript to allow it.

But that's not the main problem.

  1. 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 the autoplay property on the audio element.
Answer from tylerm on Stack Overflow
🌐
npm
npmjs.com › package › @types › play-sound
@types/play-sound - npm
/// <reference types="node" /> import { ChildProcess, ExecException } from "child_process"; type Players = "mplayer" | "afplay" | "mpg123" | "mpg321" | "play" | "omxplayer" | "aplay" | "cmdmp3"; interface PlayOpts { players: Players[]; player: Players; } type PlayMethodOptions = Partial< { [key in Players]: Array<string | number>; } & { timeout: number; } >; declare class Play { constructor(opts?: Partial<PlayOpts>); player: unknown; players: Players[]; urlRegex: RegExp; play(what: string, options?: PlayMethodOptions, next?: (err: ExecException) => void): ChildProcess; play(what: string, next?: (err: ExecException) => void): ChildProcess; test(next?: (err: ExecException) => void): ChildProcess; } declare function defaultExport(opts?: Partial<PlayOpts>): Play; export = defaultExport;
      » npm install @types/play-sound
    
🌐
CodeSandbox
codesandbox.io › s › typescript-audio-player-xvsx8
Typescript Audio Player - CodeSandbox
November 9, 2020 - Typescript Audio Player by iamBevan using @material-ui/core, @material-ui/icons, moment, node-sass, react, react-dom, react-scripts
Published   Nov 08, 2020
Author   iamBevan
🌐
TypeScript
typescriptlang.org › play › typescript › language › soundness.ts.html
TypeScript: Playground Example - Soundness
interface InputEvent { timestamp: number; } interface MouseInputEvent extends InputEvent { x: number; y: number; } interface KeyboardInputEvent extends InputEvent { keyCode: number; } function listenForEvent(eventType: "keyboard" | "mouse", handler: (event: InputEvent) => void) { } // You can re-declare the parameter type to be a subtype of the declaration. Above, handler expected a type InputEvent but in the below usage examples - TypeScript accepts a type which has additional properties.
🌐
YouTube
youtube.com › typescript games
Tutorial - Playing Sounds in TypeScript - YouTube
Learn to play sounds using typescript. For this tutorial I am using TypeScript to play sounds in the web browser. This can be very useful for playing sounds ...
Published   May 19, 2016
Views   6K
🌐
Ourcade
examples.ourcade.co › phaser3-typescript › audio › web-audio › play-sound-on-keypress
Play Sound On Keypress Example for Phaser 3 in TypeScript - Ourcade: Playful Game Development
JavaScript and TypeScript game development code examples in Phaser 3 for a kind and helpful community for open-minded and optimistic learners and developers
🌐
npm
npmjs.com › package › play-sound
play-sound - npm
August 24, 2023 - var player = require('play-sound')(opts = {}) // $ mplayer foo.mp3 player.play('foo.mp3', function(err){ if (err) throw err }) // { timeout: 300 } will be passed to child process player.play('foo.mp3', { timeout: 300 }, function(err){ if (err) throw err }) // configure arguments for executable if any player.play('foo.mp3', { afplay: ['-v', 1 ] /* lower volume for afplay on OSX */ }, function(err){ if (err) throw err }) // access the node child_process in case you need to kill it on demand var audio = player.play('foo.mp3', function(err){ if (err && !err.killed) throw err }) audio.kill()
      » npm install play-sound
    
Published   Aug 24, 2023
Version   1.1.6
Author   Hrvoje Simic
🌐
DEV Community
dev.to › upsilon_it › how-we-developed-an-online-music-player-with-typescript-2o9n
How We Developed an Online Music Player with TypeScript - DEV Community
June 7, 2021 - In this article, we share our experience of creating a custom music player for a Social Music Web Platform with TypeScript illustrating each step with a programming code.
Find elsewhere
🌐
Restack
restack.io › p › ai-driven-audio-engineering-typescript-answer-cat-ai
Typescript Play Audio Tools | Restackio
Below is a detailed breakdown of how to utilize this module with TypeScript. ... This import statement is crucial as it gives you access to the audio functionalities needed for playback. To play audio, you first need to create an instance of the Audio.Sound class.
🌐
Stack Exchange
gamedev.stackexchange.com › questions › 159332 › playing-background-music
typescript - Playing Background music - Game Development Stack Exchange
June 1, 2018 - I'm not sure how I should be doing it if I want to start when the game starts but have a delay until the song is played and than get called again. ... \$\begingroup\$ Could you provide some information about what language you are using, and what libraries to handle sound? \$\endgroup\$ ... \$\begingroup\$ I'm using typescript and to play the sound it's just an object from javascript i think \$\endgroup\$
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLAudioElement › Audio
HTMLAudioElement: Audio() constructor - Web APIs | MDN
The Audio() constructor creates and returns a new HTMLAudioElement which can be either attached to a document for the user to interact with and/or listen to, or can be used offscreen to manage and play audio.
🌐
GitHub
github.com › compodoc › typescript-ast-soundtrack
GitHub - compodoc/typescript-ast-soundtrack: Play sound with the AST of a typescript file
Play sound with the AST of a typescript file. Contribute to compodoc/typescript-ast-soundtrack development by creating an account on GitHub.
Author   compodoc
🌐
GitHub
github.com › topics › audio-player
Build software better, together
react hooks typescript music-player html5 audio-player ... react audio hooks hook player sound audio-player web-audio controls html5-audio web-audio-library howler web-audio-api react-hooks react-hook audio-player-library browser-audio react-howler
🌐
GitHub
github.com › EvandroLG › ts-audio
GitHub - EvandroLG/ts-audio: :musical_score: ts-audio is an agnostic library that makes it easy to work with AudioContext and create audio playlists in the browser
ts-audio is a lightweight, agnostic, and easy-to-use TypeScript/JavaScript library that simplifies working with the Web Audio API (AudioContext) and provides powerful playlist management capabilities.
Starred by 341 users
Forked by 25 users
Languages   TypeScript 97.2% | JavaScript 2.6% | Shell 0.2% | TypeScript 97.2% | JavaScript 2.6% | Shell 0.2%
🌐
npm
npmjs.com › package › @types › audio-play
@types/audio-play - npm
November 6, 2023 - /** * Creates and plays/pauses a sound effect or music.
      » npm install @types/audio-play
    
🌐
GitHub
github.com › Microsoft › TypeScript › issues › 24569
Typescript Audio() object not taking NodeRequire as parameter · Issue #24569 · microsoft/TypeScript
June 1, 2018 - Actual behavior: So, putting this logic in Typescript, this will work without a type error: const htmlaudio: HTMLAudioElement = new Audio(require('./assets/song.mp3')); htmlaudio.play(); However, if the parameter is explicitly declared to be type NodeRequire, the code will still work but will flag a type error: const audio: NodeRequire = require('./assets/song.mp3'); const htmlaudio: HTMLAudioElement = new Audio(audio); htmlaudio.play(); Argument of type 'NodeRequire' is not assignable to parameter of type 'string | undefined'.
Author   phinguyen1618
🌐
Webdevtutor
webdevtutor.net › blog › typescript-audio-play
A Guide to Audio Playback in TypeScript
This code snippet creates a new Audio object with the specified audio file and plays it immediately. You can also pause, stop, and control the playback using methods like pause() and currentTime. For more advanced audio control, you can leverage the Web Audio API in TypeScript.
🌐
Stack Overflow
stackoverflow.com › questions › 54840012 › how-to-play-audio-for-browse-files-in-html5-using-typescript
angular - How to play audio for browse files in html5 using typescript? - Stack Overflow
<input type="file" id="soundUrl" (change)="onAudioPlay()" /> <audio id="sound" controls></audio> above is my HTML code and i need to play audio after selecting mp3 file using typescript. angular · html · typescript · Share · Improve this question · Follow · edited Feb 23, 2019 at 9:10 ·
🌐
GitHub
github.com › joshwcomeau › use-sound
GitHub - joshwcomeau/use-sound: A React Hook for playing sound effects · GitHub
If your project uses TypeScript, you should also install the @types/howler package as a dev dependency. The tutorial includes many demos, as well as instructions for finding and preparing sound effects. It's a great place to start. You can also view the storybook, which includes lots of quick examples. import useSound from 'use-sound'; import boopSfx from '../../sounds/boop.mp3'; const BoopButton = () => { const [play] = useSound(boopSfx); return <button onClick={play}>Boop!</button>; }; This demo only plays the sound while hovering over an element.
Starred by 3.1K users
Forked by 108 users
Languages   JavaScript 64.1% | TypeScript 35.9%