How to Bundle Audio File into JS App

By using the HTML audio element, we can embed sound in documents. When your app in running in a server, we can add audio files and refer to the paths in the audio element’s src attribute.

What if you are creating an JS bundle that can be injected into a web site as a script tag? In this post, we will look into how this can be done.

The quickest way is to convert mp3 file into a base64 encoded string, then add the string in HTML. In this way, the bundle can include audio.

I learned this trick from a friend of mine, who also has a blog site called mydevhack. It’s got a lot of cool front end hack. You should check it out.

Example

I made a bell that rings. This is a single JavaScript bundle that I am injecting into this div. You can see the base64 encoded string in the bundle here. The source code can be found here.

Tap the bell to ring!

Steps

  • Convert mp3 into base64 encoded string

Once you have an audio file, I recommend you to edit the file with an audio editing tool to trim the unnecessary leading and trailing part and export it with lower quality mp3. You don’t need to have a fancy tool like ProTools or Ableton. You can just download an open source audio editor like Audacity.

Compressing audio files is an important step because we want to minimise the size of the bundle. Another technique is to make the audio file as mono instead of stereo to reduce the size. The bigger the file is the bigger the base64 encoded string will be. If you want to go for the quality, It is probably better to have a good quality audio file stored somewhere instead of bundling it.

If you are in a hurry, the compression step doesn’t need to happen. You can just convert mp3 to base64 by using a website offers free mp3 to base64 conversion service like Base64Guru.

  • Add the base64 encoded string into HTML

Once you convert the audio into base64 encoded string, all you need to do is to include it in the source tag inside the audio tag. You can have a constant variable and import it into the part where you want to use the audio

  • Add JavaScript to play sound

Then you can simply add JavaScript to trigger play.

Here is an example code with Audio embedded with the function to trigger audio play. Make sure to prepend data:audio/mpeg;base64, to the base64 encoded audio text.

Front-End
Unable to Get Local Issuer Certificate for installing Npm Modules

unable to get local issuer certificate error with yarn install from a private npm repository (such as JFrog), you can try running this command: yarn config set “strict-ssl” false yarn install Error message example error An unexpected error occurred: “https://npco.jfrog.io/artifactory/api/npm/npm-mdh/@mdh/my-library/-/@mdh/my-library-3.21.0.tgz: unable to get local issuer certificate”.

Front-End
Moving away from React.FC and React.VFC

Since the release of React 18, VFC has been deprecated. FunctionalComponent (FC) does not have implicit children any more. See this pull request. It states: Since the release of the React 18 types we haven’t seen any use case for FunctionComponent with implicit children beyond "it breaks existing stuff". As …

Front-End
How to fix react-day-picker flickering hover state between mouseenter and mouseleave

I had an issue with react-day-picker flickering hover state between mouseenter and mouseleave when the cursor was on the edge of the day I was trying to select. This is not a bug in the library. It was the custom style I added to the hover state that was causing …