Why doesn't my Javascript program work with the code provided in the NPM package's README? #30
Replies: 1 comment
-
@TheNoceboEffect, based on your errors I assume you are coding in Javascript and NOT Typescript The errors you got just tell you that the Type annotations you attempt to use in your code (from the Readme) can not get parsed So what you need to do is remove everything related to typescript If you use the code from the Readme, this gives you something like this: import { PlayerCore } from 'web-audio-api-player'
const initializePlayer = () => {
const options = {
soundsBaseUrl: 'https://raw.githubusercontent.com/chrisweb/chrisweb/main/public/',
loopQueue: true,
}
const player = new PlayerCore(options)
const myPlaylist = []
// add a first song named track01
myPlaylist.push(addSong('track01', 1, player))
// add a second song named track01
myPlaylist.push(addSong('track01', 2, player))
return player
}
const addSong = (name, id, player) => {
const firstSongAttributes = {
source: [
{
url: 'assets/music/' + name + '.mp3',
codec: 'mp3',
},
{
url: 'assets/music/' + name + '.ogg',
codec: 'ogg',
isPreferred: true,
}
],
id: id,
}
const song = player.addSoundToQueue({ soundAttributes: firstSongAttributes })
return song
}
// prepare the player, by creating and instance and adding the songs
const player = initializePlayer()
// when a user interacts with your play button
player.play() this is pseudo code, I did not actually test this code, it is just to give you an idea of what to do, you need to adapt this code a bit to your environment If you want, I could have a quick look at your code, but you would need to put some code and sound files into a public repository public and then I could fork it to try out your code and then eventually be able to give you some feedback |
Beta Was this translation helpful? Give feedback.
-
I am wondering if there are any errors with the code of my Javascript program, which used the code that was provided in the NPM package, being,
const player = new PlayerCore(options)
andconst firstSongAttributes: ISoundAttributes = { source: [ { url: 'mp3/song1.mp3', codec: 'mp3', }, { url: 'ogg/song2.ogg', codec: 'ogg', isPreferred: true, } ], id: 1, }
.I put
const player = new PlayerCore(options)
as the first line of my script, and replaced the audio URL with the Github repository URL to my sounds, and made sure to not put the entire URL of the sound to play in the second 'url' instance, and just include the part of the file directory where the sound I wanted to play was. However, when I made the script to play a sound when it was executed, I tried the program, and no sound played. However, I checked my text editor (Visual Studio Code), and it displayed two errors, which were "Type annotations can only be used in TypeScript files.", on the lines of code containing "ICoreAttributes" and "ISoundAttributes" respectively.Is it an error in the code, do I need to include more code that was included in the "README" file of the NPM package, or could it be something else, like needing to install or import Typescript somewhere in my code (which I doubt, considering that this package is intended to be used in Javascript files)?
Beta Was this translation helpful? Give feedback.
All reactions