How to use the react-native-sound function in react-native-sound

To help you get started, we’ve selected a few react-native-sound examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github okoala / 30DaysofReactNative / Project08-RandomGradientColorMusic / index.js View on Github external
_playMusic () {
    const self = this

    if (!this.music) {
      this.music = new Sound('Ecstasy.mp3', Sound.MAIN_BUNDLE, (error) => {
        if (error) {
          console.log('failed to load the sound', error)
        } else { // loaded successfully
          console.log('duration in seconds: ' + self.music._duration +
              'number of channels: ' + self.music._numberOfChannels)
          // 无线循环播放
          self.music.setNumberOfLoops(-1).play()
        }
      })
    } else {
      this.music.play()
    }
  }
github EdgeApp / edge-react-gui / src / lib / soundsMiddleware.js View on Github external
// @flow

import Sound from 'react-native-sound'

import { OPEN_AB_ALERT } from '../../src/constants/ActionConstants'
import type { Action } from '../modules/ReduxTypes.js'
import { DISPLAY_TRANSACTION_ALERT } from '../modules/UI/components/TransactionAlert/actions'

const AUDIO_RECEUVED_FILE = 'audio_received.mp3'
const AUDIO_SEND_FILE = 'audio_sent.mp3'

Sound.setCategory('Ambient')

const audioReceived = new Sound(AUDIO_RECEUVED_FILE, Sound.MAIN_BUNDLE, error => {
  if (error) console.warn(`failed to load the sound ${AUDIO_RECEUVED_FILE}`, error)
})

const audioSent = new Sound(AUDIO_SEND_FILE, Sound.MAIN_BUNDLE, error => {
  if (error) console.error(`failed to load the sound ${AUDIO_SEND_FILE}`, error)
})

const playCb = audioName => success => {
  if (success) {
    console.info(`successfully finished playing ${audioName}`)
  } else {
    console.warn(`playback ${audioName} failed due to audio decoding errors `)
  }
}

export default () => (next: Function) => (action: Action) => {
github zmxv / react-native-sound-demo / main.js View on Github external
return;
    }
    setTestState(testInfo, component, 'playing');
    // Run optional pre-play callback
    testInfo.onPrepared && testInfo.onPrepared(sound, component);
    sound.play(() => {
      // Success counts as getting to the end
      setTestState(testInfo, component, 'win');
      // Release when it's done so we're not using up resources
      sound.release();
    });
  };

  // If the audio is a 'require' then the second parameter must be the callback.
  if (testInfo.isRequire) {
    const sound = new Sound(testInfo.url, error => callback(error, sound));
  } else {
    const sound = new Sound(testInfo.url, testInfo.basePath, error => callback(error, sound));
  }
}
github duytq94 / react-native-fb-reactions-animation / App / Containers / Animation / Animation.Screen.js View on Github external
constructor (props) {
    super(props)
    this.backPress = this.handleBackPress.bind(this)

    this.soundBoxDown = new Sound('box_down.mp3', Sound.MAIN_BUNDLE, (error) => { })
    this.soundBoxUp = new Sound('box_up.mp3', Sound.MAIN_BUNDLE, (error) => { })
    this.soundIconChoose = new Sound('icon_choose.mp3', Sound.MAIN_BUNDLE, (error) => { })
    this.soundIconFocus = new Sound('icon_focus.mp3', Sound.MAIN_BUNDLE, (error) => { })
    this.soundShortTouchLike = new Sound('short_press_like.mp3', Sound.MAIN_BUNDLE, (error) => { })

    // Slow down speed animation here (1 = default)
    this.timeDilation = this.props.navigation.state.params.speed

    // If duration touch longer than it, mean long touch
    this.durationLongPress = 250

    // Variables to check
    // 0 = nothing, 1 = like, 2 = love, 3 = haha, 4 = wow, 5 = sad, 6 = angry
    this.isTouchBtn = false

    this.isLongTouch = false
    this.isLiked = false
    this.whichIconUserChoose = 0
    this.currentIconFocus = 0
    this.previousIconFocus = 0
github tubackkhoa / tkframework / mobile / ReactNotes / ui / containers / Youtube / youtube-player.js View on Github external
_play() {
    this._stop()
    const sound = new Sound(this.state.audioPath, '', (error) => {
      if (error) {
        console.log('failed to load the sound', error)
      }
    })

    setTimeout(() => {
      sound.play((success) => {
        if (success) {
           console.log('successfully finished playing')
         } else {
           console.log('playback failed due to audio decoding errors')
         }
      })
    }, 500)
  }
github soutot / react-native-voice-record-app / src / App.js View on Github external
setTimeout(() => {
			var sound = new Sound(this.state.audioPath + filename, '', (error) => {
				if (error) {
					//console.log('failed to load the sound', error);
				}
			});

			setTimeout(() => {
				sound.play((success) => {
					if (success) {
						//console.log('successfully finished playing');
					} else {
						//console.log('playback failed due to audio decoding errors');
					}
				});
			}, 100);
		}, 100);
	}
github EdgeApp / edge-react-gui / src / lib / soundsMiddleware.js View on Github external
import Sound from 'react-native-sound'

import { OPEN_AB_ALERT } from '../../src/constants/ActionConstants'
import type { Action } from '../modules/ReduxTypes.js'
import { DISPLAY_TRANSACTION_ALERT } from '../modules/UI/components/TransactionAlert/actions'

const AUDIO_RECEUVED_FILE = 'audio_received.mp3'
const AUDIO_SEND_FILE = 'audio_sent.mp3'

Sound.setCategory('Ambient')

const audioReceived = new Sound(AUDIO_RECEUVED_FILE, Sound.MAIN_BUNDLE, error => {
  if (error) console.warn(`failed to load the sound ${AUDIO_RECEUVED_FILE}`, error)
})

const audioSent = new Sound(AUDIO_SEND_FILE, Sound.MAIN_BUNDLE, error => {
  if (error) console.error(`failed to load the sound ${AUDIO_SEND_FILE}`, error)
})

const playCb = audioName => success => {
  if (success) {
    console.info(`successfully finished playing ${audioName}`)
  } else {
    console.warn(`playback ${audioName} failed due to audio decoding errors `)
  }
}

export default () => (next: Function) => (action: Action) => {
  if (action.type === DISPLAY_TRANSACTION_ALERT) {
    audioReceived.play(playCb(AUDIO_RECEUVED_FILE))
  }
  if (action.type === OPEN_AB_ALERT && action.data && action.data.success) {
github netease-im / NIM_ReactNative_Demo / src / components / chatMsg.js View on Github external
onPress={() => {
          if (constObj.sound) {
            constObj.sound.stop(() => {
              constObj.sound.release();
              constObj.sound = null;
            });
          } else {
            constObj.sound = new Sound(soundUrl, null, (error) => {
              if (error) {
                Alert.alert('网络不通');
                constObj.sound.release();
                constObj.sound = null;
                return;
              }
              constObj.sound.play((success) => {
                console.log(success, constObj.sound);
                if (!success) {
                  Alert.alert('文件已损坏');
                  return;
                }
                constObj.sound.release();
                constObj.sound = null;
              });
            });
github trazyn / SoundcloudMobX / src / stores / player.js View on Github external
}

        self.playing = true;
        self.paused = false;

        var song = self.song;
        var response = axios.get(song.uri + '/streams', {
            params: {
                client_id: CLIENT_ID,
            }
        }).catch(ex => {
            self.next();
        });
        var fromUrl = (await response).data.http_mp3_128_url;

        self.whoosh = new Sound(fromUrl, '', async err => {

            var whoosh = self.whoosh;

            if (err) {
                self.next();
            } else {

                var tick = 0;
                clearTimeout(self.timer);
                self.timer = setTimeout(function playing() {
                    whoosh.getCurrentTime(seconds => {
                        tick = seconds * 1000;

                        /** Sometimes the play callback is not invoked by 'react-native-sound', we need check current time */
                        if (seconds === 0) {
                            return self.next();