react-native-sound
React Native module for playing sound clips on iOS, Android, and Windows
npm downloads npm version npm license
react-native-soundSimilar Packages:
Npm Package Weekly Downloads Trend
3 Years
๐ŸŒŸ Show real-time usage chart on react-native-sound's README.md, just copy the code below.
## Usage Trend
[![Usage Trend of react-native-sound](https://npm-compare.com/img/npm-trend/THREE_YEARS/react-native-sound.png)](https://npm-compare.com/react-native-sound#timeRange=THREE_YEARS)
Cumulative GitHub Star Trend
๐ŸŒŸ Show GitHub stars trend chart on react-native-sound's README.md, just copy the code below.
## GitHub Stars Trend
[![GitHub Stars Trend of react-native-sound](https://npm-compare.com/img/github-trend/react-native-sound.png)](https://npm-compare.com/react-native-sound)
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-native-sound73,4662,86994.4 kB2378 days agoMIT
README for react-native-sound

๐ŸŽต React Native Sound

npm version license downloads TypeScript New Architecture

๐Ÿš€ Cross-platform audio playback for React Native โ€” Play sound clips on iOS and Android with full TypeScript support and modern React Native architecture compatibility.

โœจ Features

  • ๐ŸŽฏ Cross-platform: Works on iOS and Android
  • ๐Ÿ“ฑ Modern Architecture: Full support for React Native's New Architecture (TurboModules)
  • ๐Ÿ”ค TypeScript: Complete TypeScript definitions included
  • ๐ŸŽ›๏ธ Rich Controls: Play, pause, stop, seek, volume, pan, and looping
  • ๐Ÿ“ Flexible Loading: Load from app bundle, local files, or network URLs
  • ๐Ÿ”„ Multiple Players: Play multiple sounds simultaneously
  • โšก Optimized: Minimal latency with preloading support
  • ๐Ÿ›ก๏ธ Reliable: Battle-tested in production apps

๐Ÿ“Š Platform Compatibility

๐Ÿ“ Note: This library focuses on audio clips playback, not streaming. For streaming audio, consider react-native-video or other dedicated streaming solutions.

iOS Implementation: Uses AVAudioPlayer for optimal performance and compatibility.

Android Implementation: Uses MediaPlayer with proper audio focus handling.

| Feature | iOS | Android | | ---------------------------- | --- | ------- | | Loading | | Load from app bundle | โœ… | โœ… | | Load from local files | โœ… | โœ… | | Load from network URLs | โœ… | โœ… | | Playback | | Play/Pause/Stop | โœ… | โœ… | | Playback completion callback | โœ… | โœ… | | Resume playback | โœ… | โœ… | | Reset to beginning | โŒ | โœ… | | Audio Control | | Volume control | โœ… | โœ… | | Pan (L/R stereo) | โœ… | โŒ | | Playback speed | โœ… | โœ… | | System Integration | | Get system volume | โœ… | โœ… | | Set system volume | โŒ | โœ… | | Advanced Features | | Loop control | โœ… | โœ… | | Exact loop count | โœ… | โŒ | | Seek to time position | โœ… | โœ… | | Get current position | โœ… | โœ… | | Get duration | โœ… | โœ… | | Get channel count | โœ… | โŒ | | Resource Management | | Explicit resource cleanup | โœ… | โœ… |

๐Ÿ“ฆ Installation

npm

npm install react-native-sound

yarn

yarn add react-native-sound

๐Ÿ—๏ธ New Architecture Support

This library supports both the old and new React Native architecture:

  • โœ… Old Architecture: Uses traditional NativeModules
  • โœ… New Architecture: Uses TurboModules for better performance
  • โœ… Expo: Compatible with custom development builds (not Expo Go)

๐Ÿ› ๏ธ Troubleshooting

Common Issues

undefined is not an object (evaluating 'RNSound.IsAndroid')

This usually indicates a linking issue. Try:

  1. Clear build cache:

    cd android && ./gradlew cleanBuildCache
    
  2. Reset Metro cache:

    npx react-native start --reset-cache
    
  3. Clean and rebuild:

    # iOS
    cd ios && rm -rf build && cd .. && npx react-native run-ios
    
    # Android
    cd android && ./gradlew clean && cd .. && npx react-native run-android
    

iOS Build Issues

  • Ensure audio files are added to Xcode project bundle
  • Check that AVFoundation framework is linked (automatically handled by CocoaPods)

Android Build Issues

  • Place audio files in android/app/src/main/res/raw/
  • Use lowercase filenames without spaces or special characters
  • Clear build cache if encountering linking issues

Getting Help

๐ŸŽฎ Demo & Examples

Complete Example App

Check out our enhanced example app with both remote and local audio playback:

  • ๐Ÿ“ /example - Full-featured demo application
  • ๐ŸŽฏ Remote URL audio playback
  • ๐Ÿ“ฑ Local bundled audio files
  • ๐ŸŽจ Modern UI with TypeScript

Community Examples

๐Ÿš€ Quick Start

Setup Audio Files

Android

Save audio files in android/app/src/main/res/raw/:

android/app/src/main/res/raw/
โ”œโ”€โ”€ whoosh.mp3        โœ… Correct
โ”œโ”€โ”€ button_click.wav  โœ… Correct
โ””โ”€โ”€ my-sound.mp3      โŒ Use underscores: my_sound.mp3

Note: Use lowercase, underscored filenames. No subdirectories allowed.

iOS

  1. Open your project in Xcode
  2. Right-click your project โ†’ "Add Files to [PROJECT]"
  3. Select your audio files and ensure they're added to the app target

Basic Usage

import Sound from "react-native-sound";

// Enable playback in silence mode (important for iOS)
Sound.setCategory("Playback");

// Load a sound file from the app bundle
const whoosh = new Sound("whoosh.mp3", Sound.MAIN_BUNDLE, (error) => {
  if (error) {
    console.log("Failed to load the sound", error);
    return;
  }

  // Sound loaded successfully
  console.log("Duration:", whoosh.getDuration(), "seconds");
  console.log("Channels:", whoosh.getNumberOfChannels());

  // Play the sound
  whoosh.play((success) => {
    if (success) {
      console.log("Successfully finished playing");
    } else {
      console.log("Playback failed due to audio decoding errors");
    }
  });
});

// Audio controls
whoosh.setVolume(0.5); // 50% volume
whoosh.setPan(1); // Full right stereo
whoosh.setNumberOfLoops(-1); // Loop indefinitely

// Get current properties
console.log("Volume:", whoosh.getVolume());
console.log("Pan:", whoosh.getPan());
console.log("Loops:", whoosh.getNumberOfLoops());

// Seek to specific time
whoosh.setCurrentTime(2.5);

// Get current playback position
whoosh.getCurrentTime((seconds) => {
  console.log("Current time:", seconds);
});

// Control playback
whoosh.pause(); // Pause playback
whoosh.stop(() => {
  // Stop and rewind
  whoosh.play(); // Play from beginning
});

// Always release resources when done
whoosh.release();

Advanced Examples

Loading from Different Sources

// From app bundle (most common)
const bundleSound = new Sound("sound.mp3", Sound.MAIN_BUNDLE, callback);

// From documents directory
const docSound = new Sound("sound.mp3", Sound.DOCUMENT, callback);

// From library directory
const libSound = new Sound("sound.mp3", Sound.LIBRARY, callback);

// From absolute path
const pathSound = new Sound("/path/to/sound.mp3", "", callback);

// From remote URL (iOS/Android only)
const urlSound = new Sound("https://example.com/sound.mp3", "", callback);

React Hook Example

import { useEffect, useRef, useState } from "react";
import Sound from "react-native-sound";

const useSound = (filename: string) => {
  const sound = useRef<Sound | null>(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const [isPlaying, setIsPlaying] = useState(false);

  useEffect(() => {
    sound.current = new Sound(filename, Sound.MAIN_BUNDLE, (error) => {
      if (error) {
        console.log("Error loading sound:", error);
        return;
      }
      setIsLoaded(true);
    });

    return () => {
      sound.current?.release();
    };
  }, [filename]);

  const play = () => {
    if (sound.current && isLoaded) {
      sound.current.play((success) => {
        setIsPlaying(false);
      });
      setIsPlaying(true);
    }
  };

  const stop = () => {
    if (sound.current) {
      sound.current.stop();
      setIsPlaying(false);
    }
  };

  return { play, stop, isLoaded, isPlaying };
};

๐Ÿ“ Important Notes

Performance Tips

  • Preload sounds during app initialization to minimize playback delay
  • Reuse Sound instances for multiple playbacks of the same file
  • Avoid race conditions by ensuring sounds are loaded before calling play()

Audio Session Behavior

  • iOS: Uses AVAudioSessionCategoryAmbient to mix multiple sounds
  • Multiple playback: You can play several sound files simultaneously
  • Background audio: Configure audio categories for background playback

File Format Support

iOS (AVAudioPlayer)

Supports: AAC, AIFF, CAF, MP3, WAV, and more

Android (MediaPlayer)

Supports: 3GPP, MP4, MP3, AAC, OGG, FLAC, WAV, and more

Path Handling

  • Android absolute paths: Use /sdcard/ prefix (e.g., /sdcard/Downloads/sound.mp3)
  • Method chaining: Supported for setters (e.g., sound.setVolume(0.5).setPan(0.5).play())

๐ŸŽต Audio Ecosystem

Related Libraries

| Library | Purpose | Best For | | -------------------------------------------------------------------------------------------------------- | ----------------------- | ---------------------------- | | react-native-video | Video & audio streaming | Streaming audio/video | | react-native-audio-toolkit | Advanced audio features | Recording & complex audio | | Expo Audio | Expo audio solution | Expo managed workflow | | @react-native-async-storage/async-storage | Storage | Persisting audio preferences |

When to Use react-native-sound

  • โœ… Playing sound effects and short audio clips
  • โœ… Background music with simple controls
  • โœ… Audio feedback for user interactions
  • โœ… Cross-platform compatibility requirements
  • โœ… TypeScript projects requiring type safety

When to Consider Alternatives

  • โŒ Audio streaming or long-form content
  • โŒ Advanced audio processing or effects
  • โŒ Audio recording capabilities
  • โŒ Complex playlist management

๐Ÿค Contributing

We welcome contributions! Here's how you can help:

Areas Where We Need Help

  • ๐Ÿ› Bug fixes and stability improvements
  • ๐Ÿ“š Documentation improvements and examples
  • ๐Ÿงช Test coverage expansion
  • ๐Ÿš€ Performance optimizations
  • ๐Ÿ†• New platform support

Development Setup

  1. Fork and clone the repository
  2. Install dependencies: npm install
  3. Run the example app: cd example && npm install && npm run android
  4. Make your changes and test thoroughly
  5. Add tests for new features
  6. Update documentation as needed

Pull Request Guidelines

  • ๐Ÿ” Open an issue first for major changes to discuss the approach
  • โœ… Include tests for new functionality
  • ๐Ÿ“ Update documentation including TypeScript definitions
  • ๐Ÿงช Test on multiple platforms (iOS and Android)
  • ๐Ÿ“ฑ Test with both architectures (old and new React Native architecture)

Code Style

  • Follow existing TypeScript/JavaScript patterns

  • Use meaningful commit messages

  • Keep changes focused and atomic

  • To minimize playback delay, you may want to preload a sound file without calling play() (e.g. var s = new Sound(...);) during app initialization. This also helps avoid a race condition where play() may be called before loading of the sound is complete, which results in no sound but no error because loading is still being processed.

  • You can play multiple sound files at the same time. Under the hood, this module uses AVAudioSessionCategoryAmbient to mix sounds on iOS.

  • You may reuse a Sound instance for multiple playbacks.

  • On iOS, the module wraps AVAudioPlayer that supports aac, aiff, mp3, wav etc. The full list of supported formats can be found at https://developer.apple.com/library/content/documentation/MusicAudio/Conceptual/CoreAudioOverview/SupportedAudioFormatsMacOSX/SupportedAudioFormatsMacOSX.html

  • On Android, the module wraps android.media.MediaPlayer. The full list of supported formats can be found at https://developer.android.com/guide/topics/media/media-formats.html

  • On Android, the absolute path can start with '/sdcard/'. So, if you want to access a sound called "my_sound.mp3" on Downloads folder, the absolute path will be: '/sdcard/Downloads/my_sound.mp3'.

  • You may chain non-getter calls, for example, sound.setVolume(.5).setPan(.5).play().

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐ŸŒŸ Support the Project

If this library helps your project, consider:

  • โญ Starring the repository
  • ๐Ÿ› Reporting bugs and issues
  • ๐Ÿ“ Contributing improvements
  • ๐Ÿ’ฌ Helping others in discussions
  • ๐Ÿ“ข Sharing with the community

Made with โค๏ธ by the React Native community

Star on GitHub