-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
113 lines (103 loc) · 2.96 KB
/
Copy pathApp.js
File metadata and controls
113 lines (103 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React from 'react';
import {Platform, StyleSheet, Text, View, TouchableOpacity } from 'react-native';
import {Loudness} from 'react-native-loudness';
import { PermissionsAndroid } from 'react-native';
export default class App extends React.Component {
constructor(props){
super(props);
this.state = {
isPermissionGranted: false,
isRecordStarted: false,
loudness: -160, // Absolute silence by default
}
const loudness = new Loudness();
const requestRecordAudioPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
{
'title': 'Record Audio Permission',
'message': 'This App needs permission to record audio ' +
'so the loudness can be properly displayed.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can record audio")
this.setState({isPermissionGranted: true});
} else {
console.log("Record audio permission denied")
}
} catch (err) {
console.warn(err)
}
}
requestRecordAudioPermission();
this.onPressButton = () => {
// No permission, do nothing
if (!this.state.isPermissionGranted) return;
if (!this.state.isRecordStarted){
// Has permission and not started yet, start a getter
loudness.start();
this.setState({isRecordStarted: true,});
this.loudnessGetter = setInterval(() => {
loudness.getLoudness((loudness) => {
// console.log(loudness);
this.setState({loudness: loudness,});
});
}, 100);
} else {
// Stop and clear the getter
loudness.stop();
this.setState({isRecordStarted: false,});
clearInterval(this.loudnessGetter);
}
}
this.getButtonText = () => {
if (!this.state.isPermissionGranted){
return 'No Permission';
}
if (this.state.isRecordStarted) {
return 'Stop';
}
return 'Start';
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>Loudness</Text>
<Text style={styles.number}>{this.state.loudness.toFixed(2)}</Text>
<TouchableOpacity onPress={this.onPressButton}>
<View style={styles.button}>
<Text style={styles.title}>{this.getButtonText()}</Text>
</View>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
},
button: {
width: 200,
height: 50,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'grey',
borderRadius: 5,
},
title: {
fontSize: 20,
textAlign: 'center',
},
number: {
fontSize: 20,
textAlign: 'center',
color: 'red',
},
});