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
🌐
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. One of our clients, a Music Tech Company, came with a request to develop a web version of their social music platform. They wanted to have an online music player that would provide advanced functionality and responsive design.
🌐
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
    
🌐
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.
🌐
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
🌐
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
🌐
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
🌐
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
:musical_score: ts-audio is an agnostic library that makes it easy to work with AudioContext and create audio playlists in the browser - EvandroLG/ts-audio
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%
Find elsewhere
🌐
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
🌐
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.
🌐
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\$
🌐
Tone.js
tonejs.github.io
Tone.js
Sound generation is not limited to synthesized sounds. You can also load a sample and play that back in a number of ways.
🌐
Medium
mcasimirian.medium.com › playing-audio-on-click-in-your-javascript-app-72aa955068fc
Playing Audio on Click in Your JavaScript App | by Manon Sainton | Medium
May 12, 2021 - You may have to create a free account before having access to sounds. Choose something that doesn’t have a delay at the start and is decent quality. For this small project, I chose two sounds: one clip that says “Play” and another that says “Stop”. Now let’s get some code into the editor!
🌐
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 › 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
🌐
Howlerjs
howlerjs.com
howler.js - JavaScript audio library for the modern web
howler.js makes working with audio in JavaScript easy and reliable across all platforms.
🌐
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.
Starred by 3.1K users
Forked by 108 users
Languages   JavaScript 64.1% | TypeScript 35.9%