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.
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
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');
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 ...
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
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
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
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.
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();
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 › 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
» npm install @types/audio-play
Published Nov 06, 2023
Version 2.3.2
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%