Skip to content

Commit 2a6ed2d

Browse files
committed
feat: add example
1 parent 4dd1982 commit 2a6ed2d

File tree

5 files changed

+91
-43
lines changed

5 files changed

+91
-43
lines changed

README.md

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,57 @@ With this library, unleash creativity by generating captivating audio waveforms
3434
## Usage 🎶
3535

3636
```js
37-
import React, { useState } from 'react';
38-
39-
import { Alert, ScrollView, StyleSheet, View } from 'react-native';
40-
import { analyzeAudio, scale, sample, AmplitudeData } from 'react-native-audio-analyzer';
37+
import { analyzeAudio, scale, sample } from 'react-native-audio-analyzer';
38+
import ReactNativeBlobUtil from 'react-native-blob-util';
4139

4240
export default function App() {
43-
const [result, setResult] = useState<AmplitudeData[]>([]);
44-
45-
useEffect(() => {
46-
// Local location on disk. !Important!: You must take care of downloading the file and give the application reading permissions
47-
analyzeAudio('<uri to audio file>')
48-
.then(res => setResult(res))
49-
.catch(err => {
50-
// handle error
51-
});
41+
const [result, setResult] = useState([]);
42+
43+
const start = useCallback(async () => {
44+
try {
45+
const response = await ReactNativeBlobUtil.config({
46+
fileCache: true,
47+
}).fetch(
48+
'GET',
49+
'https://file-examples.com/storage/fe61380d0265a2c7a970ef9/2017/11/file_example_WAV_10MG.wav',
50+
{}
51+
);
52+
const path = response.path();
53+
const data = await analyzeAudio(path);
54+
setResult(data);
55+
} catch (error) {
56+
console.log(error);
57+
}
5258
}, []);
5359

5460
return (
5561
<View style={styles.container}>
62+
<Button title="Start" onPress={start} />
5663
<ScrollView horizontal style={styles.scroll}>
5764
<View style={styles.row}>
58-
{result.length > 0 && scale(result.map(_ => _.amplitude)).map((value, index) => (
59-
<View
60-
key={index}
61-
style={[styles.item, { height: value * 100 }]}
62-
/>
63-
))}
65+
{result.length > 0 &&
66+
scale(result.map((_) => _.amplitude)).map((value, index) => (
67+
<View
68+
key={index}
69+
style={[styles.item, { height: value * 100 }]}
70+
/>
71+
))}
6472
</View>
6573
</ScrollView>
6674
<ScrollView horizontal style={styles.scroll}>
6775
<View style={styles.row}>
68-
{result.length > 0 && scale(
69-
sample(result.map(_ => _.amplitude), 20)).map((value, index) => (
70-
<View
71-
key={index}
72-
style={[styles.item, { height: value * 100 }]}
73-
/>
74-
))}
76+
{result.length > 0 &&
77+
scale(
78+
sample(
79+
result.map((_) => _.amplitude),
80+
20
81+
)
82+
).map((value, index) => (
83+
<View
84+
key={index}
85+
style={[styles.item, { height: value * 100 }]}
86+
/>
87+
))}
7588
</View>
7689
</ScrollView>
7790
</View>
@@ -100,6 +113,7 @@ const styles = StyleSheet.create({
100113
},
101114
});
102115

116+
103117
```
104118

105119
## Contributing 🤝

example/ios/Podfile.lock

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,12 @@ PODS:
376376
- React-jsinspector (0.72.6)
377377
- React-logger (0.72.6):
378378
- glog
379-
- react-native-audio-analyzer (0.1.4):
379+
- react-native-audio-analyzer (1.0.1):
380380
- ffmpeg-kit-ios-audio (= 5.1)
381381
- RCT-Folly (= 2021.07.22.00)
382382
- React-Core
383+
- react-native-blob-util (0.19.6):
384+
- React-Core
383385
- React-NativeModulesApple (0.72.6):
384386
- hermes-engine
385387
- React-callinvoker
@@ -542,6 +544,7 @@ DEPENDENCIES:
542544
- React-jsinspector (from `../node_modules/react-native/ReactCommon/jsinspector`)
543545
- React-logger (from `../node_modules/react-native/ReactCommon/logger`)
544546
- react-native-audio-analyzer (from `../..`)
547+
- react-native-blob-util (from `../node_modules/react-native-blob-util`)
545548
- React-NativeModulesApple (from `../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`)
546549
- React-perflogger (from `../node_modules/react-native/ReactCommon/reactperflogger`)
547550
- React-RCTActionSheet (from `../node_modules/react-native/Libraries/ActionSheetIOS`)
@@ -625,6 +628,8 @@ EXTERNAL SOURCES:
625628
:path: "../node_modules/react-native/ReactCommon/logger"
626629
react-native-audio-analyzer:
627630
:path: "../.."
631+
react-native-blob-util:
632+
:path: "../node_modules/react-native-blob-util"
628633
React-NativeModulesApple:
629634
:path: "../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios"
630635
React-perflogger:
@@ -697,7 +702,8 @@ SPEC CHECKSUMS:
697702
React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae
698703
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
699704
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
700-
react-native-audio-analyzer: ece0398aa8d33fb066efdb7ad73debf034a846f7
705+
react-native-audio-analyzer: 8e3b3b3dfcdca007481dd0684b52ff0a94596494
706+
react-native-blob-util: d8fa1a7f726867907a8e43163fdd8b441d4489ea
701707
React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2
702708
React-perflogger: e3596db7e753f51766bceadc061936ef1472edc3
703709
React-RCTActionSheet: 17ab132c748b4471012abbcdcf5befe860660485
@@ -721,4 +727,4 @@ SPEC CHECKSUMS:
721727

722728
PODFILE CHECKSUM: d421679a07c4e143f071814d8f18bd9663a61516
723729

724-
COCOAPODS: 1.14.2
730+
COCOAPODS: 1.11.2

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"@react-native/metro-config": "^0.72.11",
2121
"babel-plugin-module-resolver": "^5.0.0",
2222
"metro-react-native-babel-preset": "0.76.8",
23-
"pod-install": "^0.1.0"
23+
"pod-install": "^0.1.0",
24+
"react-native-blob-util": "^0.19.6"
2425
},
2526
"engines": {
2627
"node": ">=16"

example/src/App.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React, { useCallback, useState } from 'react';
22

3-
import { Alert, ScrollView, StyleSheet, View } from 'react-native';
3+
import { Button, ScrollView, StyleSheet, View } from 'react-native';
44
import { analyzeAudio, scale, sample } from 'react-native-audio-analyzer';
55
import type { AmplitudeData } from 'react-native-audio-analyzer';
6+
import ReactNativeBlobUtil from 'react-native-blob-util';
67

78
export default function App() {
89
const [result, setResult] = useState<AmplitudeData[]>([]);
910

10-
useEffect(() => {
11-
analyzeAudio('<file uri>')
12-
.then((res) => {
13-
setResult(res);
14-
})
15-
.catch((err) => {
16-
if (err instanceof Error) {
17-
console.log(err);
18-
Alert.alert('Error', err.message);
19-
}
20-
});
11+
const start = useCallback(async () => {
12+
try {
13+
const response = await ReactNativeBlobUtil.config({
14+
fileCache: true,
15+
}).fetch(
16+
'GET',
17+
'https://file-examples.com/storage/fe61380d0265a2c7a970ef9/2017/11/file_example_WAV_10MG.wav',
18+
{}
19+
);
20+
const path = response.path();
21+
const data = await analyzeAudio(path);
22+
setResult(data);
23+
} catch (error) {
24+
console.log(error);
25+
}
2126
}, []);
2227

2328
return (
2429
<View style={styles.container}>
30+
<Button title="Start" onPress={start} />
2531
<ScrollView horizontal style={styles.scroll}>
2632
<View style={styles.row}>
2733
{result.length > 0 &&

yarn.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4029,6 +4029,13 @@ __metadata:
40294029
languageName: node
40304030
linkType: hard
40314031

4032+
"base-64@npm:0.1.0":
4033+
version: 0.1.0
4034+
resolution: "base-64@npm:0.1.0"
4035+
checksum: 5a42938f82372ab5392cbacc85a5a78115cbbd9dbef9f7540fa47d78763a3a8bd7d598475f0d92341f66285afd377509851a9bb5c67bbecb89686e9255d5b3eb
4036+
languageName: node
4037+
linkType: hard
4038+
40324039
"base64-js@npm:^1.1.2, base64-js@npm:^1.3.1":
40334040
version: 1.5.1
40344041
resolution: "base64-js@npm:1.5.1"
@@ -10774,6 +10781,7 @@ __metadata:
1077410781
pod-install: ^0.1.0
1077510782
react: 18.2.0
1077610783
react-native: 0.72.6
10784+
react-native-blob-util: ^0.19.6
1077710785
languageName: unknown
1077810786
linkType: soft
1077910787

@@ -10808,6 +10816,19 @@ __metadata:
1080810816
languageName: unknown
1080910817
linkType: soft
1081010818

10819+
"react-native-blob-util@npm:^0.19.6":
10820+
version: 0.19.6
10821+
resolution: "react-native-blob-util@npm:0.19.6"
10822+
dependencies:
10823+
base-64: 0.1.0
10824+
glob: ^10.3.10
10825+
peerDependencies:
10826+
react: "*"
10827+
react-native: "*"
10828+
checksum: 53403b7af91e3e5b0094ed858d35abc76093ac70c84e3101b56c6780747ef9649ffff959975a630aa739a7896e7978775b62ae3120171030aeffba6cc851ed42
10829+
languageName: node
10830+
linkType: hard
10831+
1081110832
"react-native-builder-bob@npm:^0.23.1":
1081210833
version: 0.23.1
1081310834
resolution: "react-native-builder-bob@npm:0.23.1"

0 commit comments

Comments
 (0)