Coder Social home page Coder Social logo

lludol / react-hl-audio Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 4.75 MB

React-hl-audio is a lightweight, headless React library for easy and flexible management of HTML5 audio elements, without prescribing UI design.

License: MIT License

TypeScript 100.00%

react-hl-audio's Introduction

React Headless Audio

React-hl-audio is a lightweight, headless React library for easy and flexible management of HTML5 audio elements, without prescribing UI design.


Usage

npm install react-hl-audio # or yarn add react-hl-audio or pnpm add react-hl-audio
import { AudioPlayerProvider } from 'react-hl-audio';
import Audio from './Audio';

function App() {
  return (
    <main>
      <AudioPlayerProvider>
        <Audio/>
      </AudioPlayerProvider>
    </main>
  )
}

export default App
import { type ChangeEventHandler, useCallback } from 'react';
import { AudioPlayer, useAudioPlayer } from './../../src/index';
import SampleMP3 from './sample.mp3';

function pad(string: number) {
	return (`0${string}`).slice(-2);
}

function format(seconds: number): string {
	const date = new Date(seconds * 1000);
	const hh = date.getUTCHours();
	const mm = date.getUTCMinutes();
	const ss = pad(date.getUTCSeconds());
	if (hh) {
		return `${hh}:${pad(mm)}:${ss}`;
	}
	return `${mm}:${ss}`;
}

const Audio = () => {
	const {
		playing, currentTime, progress, duration, play, pause, seekTo, loaded,
	} = useAudioPlayer();

	const onChange: ChangeEventHandler<HTMLInputElement> = useCallback((e) => {
		seekTo((duration / 100) * Number(e.target.value));
	}, [duration, seekTo]);

	return (
		<div style={{ display: 'flex', gap: 10, alignItems: 'center' }}>
			<AudioPlayer
				src={SampleMP3}
			/>

			{ !loaded && <p>Loading...</p>}

			{ loaded && <>
				<button type="button" onClick={playing ? pause : play}>
					{ playing ? "Pause" : "Play" }
				</button>
				<input type="range" min="0" max="100" value={progress} id="volume-slider" onChange={onChange} />

				<p style={{ display: 'flex', gap: 5 }}>
					<time dateTime={`P${Math.round(currentTime)}S`}>
						{format(currentTime)}
					</time>
					/
					<time dateTime={`P${Math.round(duration)}S`}>
						{format(duration)}
					</time>
				</p>
			</>}
		</div>
	);
};

export default Audio;

Props

AudioPlayer

Props Type Default Note
src string
autoPlay boolean false
onProgress (progress: number) => void In percent (%)
onFirstPlay () => void
onEnded () => void

useAudioPlayer

Props Type Default Note
loaded boolean false
playing boolean false
duration number 0 In seconds
currentTime number 0 In seconds
progress number 0 In percent (%)
play () => void
pause () => void
seekTo (time: number) => void

Contributing

Don't hesitate to create a pull request to improve the project.

Bugs

If you find a bug or want a new feature, don't hesitate to create an issue.

License

MIT

react-hl-audio's People

Contributors

lludol avatar dependabot[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.