You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

react-udux-player

Package Overview
Dependencies
Maintainers
0
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-udux-player

A customizable music player for React applications

66.0.2
latest
npmnpm
Version published
Weekly downloads
189
-66.67%
Maintainers
0
Weekly downloads
 
Created
Source

Here's the complete README.md file in one view for easy copying:

# React Udux Player

A customizable music player for React applications with HLS streaming support, track preloading, style isolation, and a rich set of features.

## Features

- 🎵 HLS streaming support
- 🔄 Track preloading for smooth transitions
- 🎨 Fully customizable theming
- 🧩 Pluggable UI components
- 📱 Responsive design
- 🔊 Volume control with mute toggle
- 🔀 Shuffle and repeat modes
- ⏱️ Progress bar with seeking
- 🎧 Event callbacks for player state changes
- 🔒 Style isolation to prevent conflicts with your app's styles

## Installation

```bash
npm install react-udux-player
# or
yarn add react-udux-player
# or
pnpm add react-udux-player

Quick Start

import { UduxPlayerProvider, UduxPlayer, useUduxPlayer } from 'react-Udux-player';

// Define your tracks
const tracks = [
  {
    id: "1",
    title: "Song Title",
    artist: "Artist Name",
    duration: 180,
    streamUrl: "https://example.com/song.m3u8",
    coverUrl: "/images/cover.jpg"
  },
  // More tracks...
];

// In your component
function App() {
  return (
    <UduxPlayerProvider>
      {/* Your app content */}
      <MusicLibrary />
      <UduxPlayer />
    </UduxPlayerProvider>
  );
}

// Use the player in any component
function MusicLibrary() {
  const { setQueue, togglePlay, currentTrack } = useUduxPlayer();
  
  return (
    <div>
      <h1>My Music</h1>
      <button onClick={() => {
        setQueue(tracks);
        togglePlay();
      }}>
        Play All
      </button>
      
      {/* Display current track */}
      {currentTrack && (
        <div>
          Now playing: {currentTrack.title} by {currentTrack.artist}
        </div>
      )}
    </div>
  );
}

Style Isolation

The player uses a scoped CSS approach to prevent style conflicts with your application. All player styles are:

  • Dynamically generated and injected at runtime
  • Prefixed with unique class names (udux-player-*)
  • Contained within the player's DOM structure
  • Automatically cleaned up when the player is unmounted

This ensures that the player's styles won't affect your application's styles and vice versa.

Customization

Theming

You can customize the player's appearance by providing a theme object:

import { UduxPlayerProvider, UduxPlayer } from 'react-Udux-player';

const myTheme = {
  colors: {
    primary: '#1DB954',           // Main accent color
    background: '#121212',        // Player background
    text: '#FFFFFF',              // Primary text color
    textSecondary: '#B3B3B3',     // Secondary text color
    progressBar: '#535353',       // Progress bar background
    progressBarFilled: '#1DB954', // Filled part of progress bar
    controlBackground: '#282828', // Background for controls
    playerBorder: '#282828',      // Border color
  },
  sizes: {
    playerHeight: '90px',         // Height of the player
    progressBarHeight: '4px',     // Height of progress bar
    controlButtonSize: '32px',    // Size of control buttons
    volumeSliderWidth: '100px',   // Width of volume slider
    coverArtSize: '56px',         // Size of cover art
  },
  borderRadius: '4px',            // Border radius for elements
  fontFamily: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif', // Font family
};

function App() {
  return (
    <UduxPlayerProvider theme={myTheme}>
      <UduxPlayer />
    </UduxPlayerProvider>
  );
}

Custom Components

You can replace any part of the player UI with your own components:

import { UduxPlayerProvider, UduxPlayer } from 'react-Udux-player';
import MyCustomPlayButton from './MyCustomPlayButton';
import MyCustomVolumeControl from './MyCustomVolumeControl';

function App() {
  return (
    <UduxPlayerProvider
      components={{
        PlayButton: MyCustomPlayButton,
        VolumeControl: MyCustomVolumeControl,
      }}
    >
      <UduxPlayer />
    </UduxPlayerProvider>
  );
}

Example of a custom play button component:

// MyCustomPlayButton.jsx
import React from 'react';

const MyCustomPlayButton = ({ isPlaying, onClick, isBuffering }) => {
  return (
    <button
      onClick={onClick}
      style={{
        backgroundColor: '#1DB954',
        color: 'white',
        border: 'none',
        borderRadius: '50%',
        width: '40px',
        height: '40px',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        cursor: 'pointer',
      }}
    >
      {isBuffering ? (
        <span>⋯</span>
      ) : isPlaying ? (
        <span>❚❚</span>
      ) : (
        <span>▶</span>
      )}
    </button>
  );
};

export default MyCustomPlayButton;

Custom Icons and Styling

The player comes with a set of custom icons and styling out of the box:

  • Like/favorite button
  • Fullscreen toggle
  • Add to playlist button
  • Minimize player button
  • Now playing view toggle
  • Dynamic background colors based on album artwork

Dominant Color Extraction

The player automatically extracts dominant colors from the album artwork and applies them as a gradient background:

Event Callbacks

Listen to player events:

import { UduxPlayerProvider, UduxPlayer } from 'react-Udux-player';

function App() {
  return (
    <UduxPlayerProvider
      onTrackChange={(track) => console.log('Now playing:', track?.title)}
      onPlaybackStateChange={(isPlaying) => console.log('Playback:', isPlaying ? 'playing' : 'paused')}
      onError={(error) => console.error('Player error:', error)}
      onTimeUpdate={(time) => console.log('Current time:', time)}
      onQueueChange={(queue) => console.log('Queue updated:', queue.length, 'tracks')}
      onVolumeChange={(volume, isMuted) => console.log('Volume:', volume, isMuted ? '(muted)' : '')}
      onShuffleChange={(shuffleEnabled) => console.log('Shuffle:', shuffleEnabled ? 'on' : 'off')}
      onRepeatChange={(repeatMode) => console.log('Repeat mode:', repeatMode)}
      onSeek={(time) => console.log('Seeked to:', time)}
      onEnded={() => console.log('Track ended')}
      onLike={(track, isLiked) => console.log(`Track ${track?.title} ${isLiked ? 'liked' : 'unliked'}`)}
      onAddToPlaylist={(track) => console.log(`Add ${track?.title} to playlist`)}
      onFullscreenChange={(isFullscreen) => console.log(`Fullscreen: ${isFullscreen}`)}
      onViewChange={(isNowPlayingView) => console.log(`Now playing view: ${isNowPlayingView}`)}
      onMinimize={() => console.log('Player minimized')}
    >
      <UduxPlayer />
    </UduxPlayerProvider>
  );
}

Creating a Custom Player

You can create your own player UI while still using the player context:

import React from 'react';
import { UduxPlayerProvider, useUduxPlayer } from 'react-Udux-player';

// Custom player component
function CustomPlayer() {
  const {
    currentTrack,
    isPlaying,
    togglePlay,
    nextTrack,
    previousTrack,
    currentTime,
    duration,
    seek,
  } = useUduxPlayer();

  // Format time as MM:SS
  const formatTime = (time) => {
    if (!time) return '0:00';
    const minutes = Math.floor(time / 60);
    const seconds = Math.floor(time % 60);
    return `${minutes}:${seconds.toString().padStart(2, '0')}`;
  };

  if (!currentTrack) {
    return <div>No track selected</div>;
  }

  return (
    <div className="my-custom-player">
      <div className="track-info">
        <img 
          src={currentTrack.coverUrl || '/default-cover.jpg'} 
          alt={currentTrack.title} 
          className="cover-art"
        />
        <div>
          <h3>{currentTrack.title}</h3>
          <p>{currentTrack.artist}</p>
        </div>
      </div>

      <div className="controls">
        <button onClick={previousTrack}>Previous</button>
        <button onClick={togglePlay}>
          {isPlaying ? 'Pause' : 'Play'}
        </button>
        <button onClick={nextTrack}>Next</button>
      </div>

      <div className="progress">
        <span>{formatTime(currentTime)}</span>
        <input
          type="range"
          min={0}
          max={duration || 100}
          value={currentTime || 0}
          onChange={(e) => seek(Number(e.target.value))}
        />
        <span>{formatTime(duration)}</span>
      </div>
    </div>
  );
}

// Main app
function App() {
  return (
    <UduxPlayerProvider>
      <div>
        {/* Your app content */}
        <CustomPlayer />
      </div>
    </UduxPlayerProvider>
  );
}

API Reference

UduxPlayerProvider Props

PropTypeDescription
childrenReactNodeChild components
themeThemeTheme customization object
componentsCustomComponentsCustom UI components
initialVolumenumberInitial volume level (0-100)
initialShufflebooleanInitial shuffle state
initialRepeat"off""all"
onTrackChange(track: Tracknull) => void
onPlaybackStateChange(isPlaying: boolean) => voidCalled when playback starts or stops
onError(error: Error) => voidCalled when an error occurs
onTimeUpdate(time: number) => voidCalled when playback time updates
onQueueChange(queue: Track[]) => voidCalled when the queue changes
onVolumeChange(volume: number, isMuted: boolean) => voidCalled when volume changes
onShuffleChange(shuffleEnabled: boolean) => voidCalled when shuffle mode changes
onRepeatChange(repeatMode: RepeatMode) => voidCalled when repeat mode changes
onSeek(time: number) => voidCalled when seeking to a position
onEnded() => voidCalled when a track ends
onLike(track: Tracknull, isLiked: boolean) => void
onAddToPlaylist(track: Tracknull) => void
onFullscreenChange(isFullscreen: boolean) => voidCalled when you trigger fullscreen mode
onViewChange(isNowPlayingView: boolean) => voidCalled when you toggle now playing
onMinimize() => voidCalled when you toggle minimize player

useUduxPlayer Hook

The useUduxPlayer hook provides access to the player state and controls:

Property/MethodTypeDescription
currentTrackTracknull
queueTrack[]Current queue of tracks
isPlayingbooleanWhether playback is active
volumenumberCurrent volume (0-100)
isMutedbooleanWhether audio is muted
currentTimenumberCurrent playback position in seconds
durationnumberDuration of current track in seconds
shufflebooleanWhether shuffle mode is active
repeat"off""all"
isBufferingbooleanWhether the player is buffering
togglePlay() => voidToggle play/pause
nextTrack() => voidSkip to next track
previousTrack() => voidGo to previous track
seek(time: number) => voidSeek to position in seconds
setVolume(volume: number) => voidSet volume (0-100)
toggleMute() => voidToggle mute
toggleShuffle() => voidToggle shuffle mode
toggleRepeat() => voidCycle repeat modes
setQueue(tracks: Track[]) => voidSet the queue of tracks
themeThemeCurrent theme object
tracksToPreloadnumber[]Indices of tracks being preloaded

Track Interface

interface Track {
  id: string;
  title: string;
  artist: string;
  album?: string;
  duration: number;
  coverUrl?: string;
  streamUrl: string;
}

Theme Interface

interface Theme {
  colors: {
    primary: string;
    background: string;
    text: string;
    textSecondary: string;
    progressBar: string;
    progressBarFilled: string;
    controlBackground?: string;
    playerBorder?: string;
  };
  sizes: {
    playerHeight: string;
    progressBarHeight: string;
    controlButtonSize: string;
    volumeSliderWidth?: string;
    coverArtSize?: string;
  };
  borderRadius: string;
  fontFamily?: string;
}

CustomComponents Interface

interface CustomComponents {
  PlayButton?: React.ComponentType<{ isPlaying: boolean; onClick: () => void; isBuffering: boolean }>;
  NextButton?: React.ComponentType<{ onClick: () => void; disabled: boolean }>;
  PreviousButton?: React.ComponentType<{ onClick: () => void; disabled: boolean }>;
  ShuffleButton?: React.ComponentType<{ active: boolean; onClick: () => void; disabled: boolean }>;
  RepeatButton?: React.ComponentType<{ mode: RepeatMode; onClick: () => void; disabled: boolean }>;
  VolumeControl?: React.ComponentType<{
    volume: number;
    isMuted: boolean;
    onVolumeChange: (volume: number) => void;
    onMuteToggle: () => void;
  }>;
  ProgressBar?: React.ComponentType<{
    currentTime: number;
    duration: number;
    onSeek: (time: number) => void;
    isBuffering: boolean;
  }>;
  TrackInfo?: React.ComponentType<{ track: Track | null }>;
  PlayerContainer?: React.ComponentType<{ children: React.ReactNode }>;
}

Troubleshooting

Controls not working

If the player controls are not responding:

  • Make sure you've set up a queue of tracks using setQueue
  • Check that your stream URLs are valid and accessible
  • Verify that you're using the useUduxPlayer hook within a component that's a child of UduxPlayerProvider
  • Add console logs to your click handlers to verify they're being called

Style conflicts

If you're experiencing style conflicts:

  • The player uses scoped CSS with prefixed class names (udux-player-*) to prevent conflicts
  • If conflicts persist, you can create a custom player UI using the useUduxPlayer hook

Audio not playing

If audio doesn't play:

  • Check that your stream URLs are valid and accessible
  • Verify that the browser supports HLS playback (most modern browsers do)
  • Check browser console for any errors
  • Try using a public test stream like https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8

Browser Support

The player works in all modern browsers that support HTML5 audio and video. For HLS streaming support, it uses the react-hls-player package which provides cross-browser compatibility.

License

Keywords

react

FAQs

Package last updated on 29 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts