我正在尝试构建一个范围计时器。这基本上启动了一个计时器并在射击时停止它。为此,我想我会测量分贝,当它达到峰值时,我会停止计时器。现在我到处看看,似乎无法让它工作。
我看过的一些东西:
使用 react-native 测量音频的响度
声音计量 react-native
https://www.npmjs.com/package/react-native-sound-level
可能是因为我实现的都错了。例如componentDidMount
无法识别。可能是因为我,但我不知道。
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;
我真的希望有人可以帮助我将实时分贝记录到控制台,我们将不胜感激。提前致谢!
编辑:
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;