There are a couple of issues here.
- The
playmethod exists onHTMLAudioElementand notHTMLElement.getElementByIdreturnsHTMLElement | null, so you'd have to castmytrackasHTMLAudioElementfor 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 theautoplayproperty on theaudioelement.
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
Published Nov 07, 2023
Version 1.1.2
Videos
02:26
How to Play Sounds in React JS - YouTube
05:43
How to Play a Sound in Angular - Angular Tutorial using the ...
06:03
How To Play Audio From An External URL On The Click Of A Button ...
10:14
Button Sound Effects Tutorial Audible HTML5 JavaScript Menu Systems ...
Playing Sounds in Your React App | Tutorial
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.
Top answer 1 of 2
5
There are a couple of issues here.
- The
playmethod exists onHTMLAudioElementand notHTMLElement.getElementByIdreturnsHTMLElement | null, so you'd have to castmytrackasHTMLAudioElementfor 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 theautoplayproperty on theaudioelement.
2 of 2
-1
//file for export
export const AudioPlay = (file) => {
new Audio(`/static/sounds/${file}`).play(); //under folder public
};
//file import
import {AudioPlay} from './comm';
//onClick event
AudioPlay('stockin-ok.mp3');
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
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
Repository https://github.com/shime/play-sound
Homepage https://github.com/shime/play-sound
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%
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.
Top answer 1 of 8
117
just did this in a project am working (angular 4) and it worked
playAudio(){
let audio = new Audio();
audio.src = "../../../assets/audio/alarm.wav";
audio.load();
audio.play();
}
this.playAudio();
make sure the path is correct and references an existing audio
2 of 8
52
As per Robin's comment, i have checked that link we can use it using the audio() object in the ts file like this:
this.audio = new Audio();
this.audio.src = "../../../assets/sounds/button_1.mp3";
this.audio.load();
this.audio.play();
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
» npm install @types/audio-play
Published Nov 06, 2023
Version 2.3.2
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
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%
Dobrian
dobrian.github.io › cmp › topics › sample-recording-and-playback-with-web-audio-api › 1.loading-and-playing-sound-files.html
Loading and Playing Sound Files
You may then play back the sound with the .play() method.