reac native play sound

Solutions on MaxInterview for reac native play sound by the best coders in the world

showing results for - "reac native play sound"
Simona
24 Feb 2020
1// Import the react-native-sound module
2var Sound = require('react-native-sound');
3
4// Enable playback in silence mode
5Sound.setCategory('Playback');
6
7// Load the sound file 'whoosh.mp3' from the app bundle
8// See notes below about preloading sounds within initialization code below.
9var whoosh = new Sound('whoosh.mp3', Sound.MAIN_BUNDLE, (error) => {
10  if (error) {
11    console.log('failed to load the sound', error);
12    return;
13  }
14  // loaded successfully
15  console.log('duration in seconds: ' + whoosh.getDuration() + 'number of channels: ' + whoosh.getNumberOfChannels());
16
17  // Play the sound with an onEnd callback
18  whoosh.play((success) => {
19    if (success) {
20      console.log('successfully finished playing');
21    } else {
22      console.log('playback failed due to audio decoding errors');
23    }
24  });
25});
26
27// Reduce the volume by half
28whoosh.setVolume(0.5);
29
30// Position the sound to the full right in a stereo field
31whoosh.setPan(1);
32
33// Loop indefinitely until stop() is called
34whoosh.setNumberOfLoops(-1);
35
36// Get properties of the player instance
37console.log('volume: ' + whoosh.getVolume());
38console.log('pan: ' + whoosh.getPan());
39console.log('loops: ' + whoosh.getNumberOfLoops());
40
41// Seek to a specific point in seconds
42whoosh.setCurrentTime(2.5);
43
44// Get the current playback point in seconds
45whoosh.getCurrentTime((seconds) => console.log('at ' + seconds));
46
47// Pause the sound
48whoosh.pause();
49
50// Stop the sound and rewind to the beginning
51whoosh.stop(() => {
52  // Note: If you want to play a sound after stopping and rewinding it,
53  // it is important to call play() in a callback.
54  whoosh.play();
55});
56
57// Release the audio player resource
58whoosh.release();