Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to play corresponding sound in react native?

galib

New Coder
JavaScript:
import React from 'react';
import {
  Text,
  View,
  SafeAreaView,
  StyleSheet,
  FlatList,
  Button,
  TouchableOpacity,
} from 'react-native';

var Sound = require('react-native-sound');

var characters = [
  'a',
  'b',
  'c',
  'd',
  'e',
  'f',
  'g',
  'h',
  'i',
  'j',
  'k',
  'l',
  'm',
  'n',
  'o',
  'p',
  'q',
  'r',
  's',
  't',
  'u',
  'v',
  'w',
  'x',
  'y',
  'z',
];

Sound.setCategory('Playback');



function playSound(item: any) {
  // Play the sound with an onEnd callback

}

function App() {
  return (
    <SafeAreaView>
      <View>
        <FlatList
          data={characters}
          renderItem={({ item }) => (
            <View style={styles.container}>
              <View
                style={{
                  padding: 10,
                }}>
                <TouchableOpacity
                  onPress={() => {
                    var whoosh = new Sound(item + '.mp3', Sound.MAIN_BUNDLE, (error: any) => {
                      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(),
                      );
                    });
                    whoosh.play((success: any) => {
                      if (success) {
                        console.log('successfully finished playing');
                      } else {
                        console.log('playback failed due to audio decoding errors');
                      }
                    });

                  }}
                  style={styles.button}>
                  <Text
                    style={{
                      fontSize: 20,
                    }}>
                    {item.toUpperCase()}
                  </Text>
                </TouchableOpacity>
              </View>
              <View
                style={{
                  padding: 10,
                }}>
                <TouchableOpacity style={styles.button}>
                  <Text
                    style={{
                      fontSize: 20,
                    }}>
                    {item}
                  </Text>
                </TouchableOpacity>
              </View>
            </View>
          )}
        />
      </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    justifyContent: 'center',
  },

  button: {
    borderWidth: 1,
    borderColor: 'rgba(0,0,0,0.2)',
    alignItems: 'center',
    justifyContent: 'center',
    width: 80,
    height: 80,
    backgroundColor: '#fff',
    borderRadius: 50,
  },
});

export default App;
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom