反应-原声
反应原生模块用于在iOS,Android,和Windows上播放声音剪辑.
'注:反应-内生-声不支持流出". 更多信息见#353. 当然,如果有人愿意的话,我们会欢迎公关.
特性矩阵
| 特性 | iOS系统 | 机器人 | 窗口 |
|---|---|---|---|
| 从应用程序捆绑中装入声音 | ✓ | ✓ | ✓ |
| 从其他目录装入声音 | ✓ | ✓ | ✓ |
| 从网络装入声音 | ✓ | ✓ | |
| 播放声音 | ✓ | ✓ | ✓ |
| 回放补全调用 | ✓ | ✓ | ✓ |
| 暂停 | ✓ | ✓ | ✓ |
| 继续 | ✓ | ✓ | ✓ |
| 停下来 | ✓ | ✓ | ✓ |
| 重设 | ✓ | ||
| 释放资源 | ✓ | ✓ | ✓ |
| 获取持续时间 | ✓ | ✓ | ✓ |
| 获取频道数 | ✓ | ||
| 获取/设置音量 | ✓ | ✓ | ✓ |
| 获取系统音量 | ✓ | ✓ | |
| 设定系统音量 | ✓ | ||
| 获取/设置平板 | ✓ | ||
| 获取/设置循环 | ✓ | ✓ | ✓ |
| 获取/ 设定当前时间 | ✓ | ✓ | ✓ |
| 设置速度 | ✓ | ✓ |
安装
首先从您的应用目录安装 npm 包 :
npm install react-native-sound --save
然后自动链接它使用 :
react-native link react-native-sound
手动安装注释
详情请参见维基百科 https://github.com/zmxv/resact-native-sound/wiki/ 安装
辅助反应声
Demo项目
https://github.com/zmxv/resact-native-sound-demo (英语)
玩家
https://github.com/benevbright/ react-native-相声-玩家观
基本用途
首先,你需要添加音频文件到您的项目中.
- Android: 在目录下保存您的声音剪辑文件
android/app/src/main/res/raw。请注意,此目录中的文件必须是小写并下划线(例如我的 file name.mp3),子目录不被Android支持. - iOS: 打开 Xcode 并添加到工程中的声音文件( 右键单击工程并选择)
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();
页:1
- 为了尽量减少回放延迟, 您可能想要在不调用的情况下预装一个声音文件
play()(例如:.var s = new Sound(...);在应用初始化期间。 这也有助于避免种族条件play()在装入声音完成之前,可以调用,这导致没有声音,但没有出错,因为装入仍在处理中. - 可以同时播放多个声音文件. 在引擎盖下,这个模块使用
AVAudioSessionCategoryAmbient在iOS上混合声音. - 你可以重用一个
Sound例如,多次重播. - 在iOS上,模块包接
AVAudioPlayer支持 Aac, aiff, mp3, wav 等。 支持格式的完整列表可见于 https://developer.apple.com/library/content/documentation/MusicAudio/Conceptual/CoreAudio Overview/支持的AudioFormatsMacOSX/支持的AudioFormatsMacOSX.html 互联网档案馆的存檔,存档日期2011-12-20 - 在Android上,模块包裹
android.media.MediaPlayer。支持格式的完整列表可见于 https://developer.android.com/guide/topics/media/media-formats.html 互联网档案馆的存檔,存档日期2014-12-21 - 在Android上,绝对路径可以从"/sdcard/"开始. 所以,如果要在下載資料夾上访问一个叫"my sound.mp3"的声音,绝对路径会是:"/sdcard/Downloads/my sound.mp3".
- 举例来说,你可能会连锁非更好的电话,
sound.setVolume(.5).setPan(.5).play()。 。 。 .
