@exodus/react-native-sound

中文文档
0.13.0-exodus.2发布于 2026/04/30MITTypeScriptCommonJS零依赖近 30 天下载 1.1万

React Native module for playing sound clips on iOS, Android, and Windows

原始使用文档
文档目录8 节
  1. Feature matrix
  2. Installation
  3. Manual Installation Notes
  4. Help with React-Native-Sound
  5. Demo project
  6. Player
  7. Basic usage
  8. Notes

react-native-sound

React Native module for playing sound clips on iOS, Android, and Windows.

'NOTE: React-native-sound does not support streaming'. See #353 for more info. Of course, we would welcome a PR if someone wants to take this on.

Feature matrix

FeatureiOSAndroidWindows
Load sound from the app bundle✓✓✓
Load sound from other directories✓✓✓
Load sound from the network✓✓
Play sound✓✓✓
Playback completion callback✓✓✓
Pause✓✓✓
Resume✓✓✓
Stop✓✓✓
Reset✓
Release resource✓✓✓
Get duration✓✓✓
Get number of channels✓
Get/set volume✓✓✓
Get system volume✓✓
Set system volume✓
Get/set pan✓
Get/set loops✓✓✓
Get/set current time✓✓✓
Set speed✓✓

Installation

First install the npm package from your app directory:

npm install react-native-sound --save

Then link it automatically using:

react-native link react-native-sound

Manual Installation Notes

Please see the Wiki for these details https://github.com/zmxv/react-native-sound/wiki/Installation

Help with React-Native-Sound

  • For react-native-sound developers Gitter chat
  • For help using react-native-sound Gitter chat

Demo project

https://github.com/zmxv/react-native-sound-demo

Player

https://github.com/benevbright/react-native-sound-playerview

Basic usage

First you'll need to add audio files to your project.

  • Android: Save your sound clip files under the directory android/app/src/main/res/raw. Note that files in this directory must be lowercase and underscored (e.g. my_file_name.mp3) and that subdirectories are not supported by Android.
  • iOS: Open Xcode and add your sound files to the project (Right-click the project and select Add Files to [PROJECTNAME])
// Import the react-native-sound module
var Sound = require('react-native-sound');

// Enable playback in silence mode
Sound.setCategory('Playback');

// Load the sound file 'whoosh.mp3' from the app bundle
// See notes below about preloading sounds within initialization code below.
var whoosh = new Sound('whoosh.mp3', Sound.MAIN_BUNDLE, (error) => {
  if (error) {
    console.log('failed to load the sound', error);
    return;
  }
  // loaded successfully
  console.log('duration in seconds: ' + whoosh.getDuration() + 'number of channels: ' + whoosh.getNumberOfChannels());

  // Play the sound with an onEnd callback
  whoosh.play((success) => {
    if (success) {
      console.log('successfully finished playing');
    } else {
      console.log('playback failed due to audio decoding errors');
    }
  });
});

// Reduce the volume by half
whoosh.setVolume(0.5);

// Position the sound to the full right in a stereo field
whoosh.setPan(1);

// Loop indefinitely until stop() is called
whoosh.setNumberOfLoops(-1);

// Get properties of the player instance
console.log('volume: ' + whoosh.getVolume());
console.log('pan: ' + whoosh.getPan());
console.log('loops: ' + whoosh.getNumberOfLoops());

// Seek to a specific point in seconds
whoosh.setCurrentTime(2.5);

// Get the current playback point in seconds
whoosh.getCurrentTime((seconds) => console.log('at ' + seconds));

// Pause the sound
whoosh.pause();

// Stop the sound and rewind to the beginning
whoosh.stop(() => {
  // Note: If you want to play a sound after stopping and rewinding it,
  // it is important to call play() in a callback.
  whoosh.play();
});

// Release the audio player resource
whoosh.release();

Notes

  • 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().

版本与发布记录

@exodus/react-native-sound 共发布 4 个版本,首次发布于 2019/07/01;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.13.0-exodus.2latest2026/04/306 个月前
0.13.02021/04/135 年前
0.12.02021/04/135 年前
0.11.02019/07/017 年前

关于 @exodus/react-native-sound 的常见问题

@exodus/react-native-sound 是什么?

React Native module for playing sound clips on iOS, Android, and Windows

如何安装 @exodus/react-native-sound?

在项目目录运行 npm install @exodus/react-native-sound,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react-native(>=0.8.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@exodus/react-native-sound 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@exodus/react-native-sound 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@exodus/react-native-sound 可以商用吗?

@exodus/react-native-sound 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@exodus/react-native-sound 的下载量有多少?

根据 npm 官方下载统计,@exodus/react-native-sound 在 2026/10/14 – 2026/11/12 这 30 天内被下载 10,623 次,日均约 354 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@exodus/react-native-sound 还在维护吗?

最新版本 0.13.0-exodus.2 发布于 2026/04/30(6 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 1 个;首次发布于 2019/07/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。