-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
116 lines (99 loc) · 2.92 KB
/
App.tsx
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
114
115
116
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, { useEffect, useState, useRef } from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
useColorScheme,
View,
Button,
StyleSheet,
Text,
} from 'react-native';
import {
Colors,
} from 'react-native/Libraries/NewAppScreen';
import { VolumeController } from 'react-native-media-controller';
import Slider from '@react-native-community/slider';
function App(): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const init = useRef<boolean>(false);
const [volume, _setVolume] = useState<number>(0);
const setVolume = async (value: number) => {
VolumeController.setVolume(value);
_setVolume(value);
};
useEffect(() => {
VolumeController.getVolume().then(setVolume);
}, []);
useEffect(() => {
// Ensure that the listener is only added once
if (!init.current) {
init.current = true;
VolumeController.addListener((newVolume) => {
console.log('new volume', newVolume);
_setVolume(newVolume);
});
return;
}
}, []);
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
const textStyle = {
color: isDarkMode ? Colors.white : Colors.black,
};
return (
<SafeAreaView style={backgroundStyle}>
<StatusBar
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
backgroundColor={backgroundStyle.backgroundColor}
/>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}
>
<View style={styles.slider}>
<Slider
minimumValue={0}
maximumValue={1}
value={volume}
onValueChange={setVolume}
/>
</View>
<Text style={textStyle}>
{`Volume: ${volume}`}
</Text>
<View
style={[{
backgroundColor: isDarkMode ? Colors.black : Colors.white,
}, styles.container]}>
<Button title="Set Volume to -1" onPress={() => setVolume(-1)} />
<Button title="Set Volume to 0" onPress={() => setVolume(0)} />
<Button title="Set Volume to 0.1" onPress={() => setVolume(0.1)} />
<Button title="Set Volume to 0.45" onPress={() => setVolume(0.45)} />
<Button title="Set Volume to 0.5" onPress={() => setVolume(0.5)} />
<Button title="Set Volume to 0.123" onPress={() => setVolume(0.123)} />
<Button title="Set Volume to 1" onPress={() => setVolume(1)} />
<Button title="Set Volume to 2" onPress={() => setVolume(2)} />
<Button title="Get volume" onPress={async () => console.log(await VolumeController.getVolume())} />
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
gap: 8,
flex: 1,
},
slider: {
padding: 16,
},
});
export default App;