简体   繁体   English

在 react native expo 中测量分贝

[英]Measuring decibels in react native expo


I am trying to build a range timer.我正在尝试构建一个范围计时器。 Which essentially starts a timer and stops it when a shot has been fired.这基本上启动了一个计时器,并在射击时停止它。 For this I thought I would measure decibels and when it spikes I stop the timer.为此,我想我会测量分贝,当它出现峰值时,我会停止计时器。 Now I have had a look around everywhere and can't seem to get it to work.现在我四处看看,似乎无法让它发挥作用。

Some of the stuff I looked at:我看过的一些东西:
Measure loudness of the audio with react-native 使用 react-native 测量音频的响度
Sound metering react-native 声音计量反应原生
https://www.npmjs.com/package/react-native-sound-level https://www.npmjs.com/package/react-native-sound-level

It could be because I implemented it all wrong.可能是因为我把它全部实现了。 For instance componentDidMount can't be recognised.例如componentDidMount无法识别。 It may be because of me but I don't know.可能是因为我,但我不知道。

import React, {
  useState,
  useEffect,
  componentDidMount,
  componentWillUnmount,
} from "react";
import { StyleSheet, Text, Dimensions } from "react-native";
import Title from "../components/Title";
import { Button, Input } from "react-native-elements";
import RNSoundLevel from 'react-native-sound-level'

const CreateGunScreen = ({ navigation }) => {
  const [name, setName] = useState("");
  const [errorMessage, setErrorMessage] = useState("");
 
    componentDidMount() { // The curly brace has a red line
    RNSoundLevel.start()
    RNSoundLevel.onNewFrame = (data) => {
        // see "Returned data" section below
        console.log('Sound level info', data)
    }
    }
    
    // don't forget to stop it
    componentWillUnmount() { // The curly brace has a red line
    RNSoundLevel.stop()
    }

  return (
    <>
      <Title text="Create Gun" />
      <Input
        label="name"
        value={name}
        onChangeText={setName}
        autoCapitalize="none"
        autoCorrect={false}
        containerStyle={styles.input}
      />
      {errorMessage ? (
        <Text style={styles.errorMessage}>{errorMessage}</Text>
      ) : null}
      <Button
        title="Cancel"
        type="clear"
        style={{
          alignSelf: "center",
          marginVertical: 10,
        }}
        onPress={() => navigation.navigate("Guns")}
      />
    </>
  );
};

export default CreateGunScreen;

I really hope someone can help me log realtime decibels to the console, and it will be appreciated.我真的希望有人可以帮助我将实时分贝记录到控制台,我们将不胜感激。 Thanks in advance!提前致谢!

EDIT:编辑:

import React, { useState, useEffect } from "react";
import { StyleSheet, Text, Dimensions } from "react-native";
import Title from "../components/Title";
import { Button, Input } from "react-native-elements";
import RNSoundLevel from "react-native-sound-level";

const CreateGunScreen = ({ navigation }) => {
  const [name, setName] = useState("");
  const [errorMessage, setErrorMessage] = useState("");

  async function start_measuring() {
    try {
      RNSoundLevel.start();
      RNSoundLevel.onNewFrame = (data) => {
        // see "Returned data" section below
        console.log("Sound level info", data);
      };
    } catch (err) {
      console.log(err);
    }
  }

  useEffect(() => {
    start_measuring();
    return function cleanup() {
      RNSoundLevel.stop();
    };
  });

  return (
    <>
    </>
  );
};

export default CreateGunScreen;

You have created a functional component, so the methods componentWillMount and componentWillUnmount are not available.您已经创建了一个功能组件,因此 componentWillMount 和 componentWillUnmount 方法不可用。 You can use the useEffect hook instead, see https://reactjs.org/docs/hooks-effect.html您可以改用 useEffect 钩子,请参阅https://reactjs.org/docs/hooks-effect.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM