This library provides two classes, SoundPlayer
and SoundRecorder
, for playing and recording sounds respectively.
It wrapps over the official approach, is more user-friendly and introduces an extra feature that allows for the cancellation of any ongoing sound playback.
The SoundPlayer
class allows you to play sound files. Here's an example of how to use it:
import { SoundPlayer } from 'easy-media.js';
// create a new SoundPlayer that stops when changing files
const player = new SoundPlayer("my-sound.mp3", true); // stop_on_play = true
player.play(); // plays the first file
// in case later you have to change the file
player.changeFile('path-to-another-audio-file'); // stops the first file and prepares the second one
Starts playing the sound. If the sound is already playing, it stops and prepares the sound again.
Stops the sound. If the sound is playing, it stops the sound and releases the player.
Changes the sound file to play.
Destroys the player. If the sound is playing, it stops the sound and removes event listeners.
The SoundRecorder class allows you to record sounds. Here’s an example of how to use it:
import { SoundRecorder } from 'easy-media.js';
const recorder = new SoundRecorder('mic-recording.opus');
recorder.start();
Starts recording.
Stops recording.
Changes the target file for the recording.