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
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
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
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.
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
YouTube
youtube.com › watch
How to Play a Sound in Angular - Angular Tutorial using the Javascript Audio Object - YouTube
If you liked the content, please consider checking out my Patreon! - https://www.patreon.com/CodingUnderPressure/membership Hey everyone, today we are going ...
Published September 14, 2021
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%
GitHub
github.com › topics › audio-player
Build software better, together
music youtube angular video-player soundcloud soundcloud-api audio-player backbonejs mixcloud cloudplayer youtube-api-v3 cloud-player ... Audiophile music player with gapless playback, parametric EQ, AutoEQ import, and real-time DSP visualizers · electron audio music typescript music-player cross-platform audio-player audiophile
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
npm
npmjs.com › package › @types › audio-play
@types/audio-play - npm
» npm install @types/audio-play
Published Nov 06, 2023
Version 2.3.2
Webdevtutor
webdevtutor.net › blog › typescript-play-audio-file
How to Play Audio Files in TypeScript: A Complete Guide
By creating a new Audio object and calling the play() method, you can start playing the audio file. You can also pause the audio by calling the pause() method. For more advanced audio manipulation and control, you can leverage the Web Audio API in TypeScript.
Webdevtutor
webdevtutor.net › blog › typescript-play-sound
How to Play Sound in Typescript: A Step-by-Step Guide
You can either download sound files from the web or create your own using audio editing software. Now, let's write the Typescript code to play the sound in your application. You can use the Web Audio API or HTML5 <audio> element to achieve this.