Skip to content

Commit d79ab6f

Browse files
maciejmakowski2003Maciej Makowski
andauthored
Fix/audio file and piano (#232)
* fix: fixed AudioFile.tsx and Button.tsx * fix: fixed piano example * fix: fixed dotenv * ci: yarn format * fix: requested fix --------- Co-authored-by: Maciej Makowski <maciej.makowski@swmansion.com>
1 parent ff195a1 commit d79ab6f

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

apps/common-app/src/components/Button.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@ import { colors, layout } from '../styles';
66
interface ButtonProps {
77
title: string;
88
onPress: () => void;
9+
disabled?: boolean;
10+
width?: number;
911
}
1012

11-
const Button: FC<ButtonProps> = (props) => {
12-
const { title, onPress } = props;
13-
13+
const Button: FC<ButtonProps> = ({
14+
title,
15+
onPress,
16+
disabled = false,
17+
width = 100,
18+
}) => {
1419
return (
1520
<Pressable
21+
disabled={disabled}
1622
onPress={onPress}
1723
style={({ pressed }) => [
1824
styles.button,
19-
{ backgroundColor: pressed ? `${colors.main}88` : colors.main },
25+
{
26+
backgroundColor: pressed ? `${colors.main}88` : colors.main,
27+
opacity: disabled ? 0.5 : 1,
28+
width: width,
29+
},
2030
]}
2131
>
2232
<Text style={styles.text}>{title}</Text>
@@ -28,7 +38,6 @@ const styles = StyleSheet.create({
2838
button: {
2939
padding: layout.spacing,
3040
borderRadius: layout.radius,
31-
width: 100,
3241
},
3342
text: {
3443
color: colors.white,

apps/common-app/src/examples/AudioFile/AudioFile.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ActivityIndicator } from 'react-native';
1111

1212
const AudioFile: FC = () => {
1313
const [isPlaying, setIsPlaying] = useState(false);
14+
const [isLoading, setIsLoading] = useState(false);
1415

1516
const audioContextRef = useRef<AudioContext | null>(null);
1617
const audioBufferSourceNodeRef = useRef<AudioBufferSourceNode | null>(null);
@@ -39,9 +40,10 @@ const AudioFile: FC = () => {
3940
if (result.canceled === false) {
4041
audioBufferSourceNodeRef.current?.stop();
4142
setIsPlaying(false);
42-
setAudioBuffer(null);
4343

44+
setIsLoading(true);
4445
await fetchAudioBuffer(result.assets[0].uri.replace('file://', ''));
46+
setIsLoading(false);
4547
}
4648
} catch (error) {
4749
console.error('Error picking file:', error);
@@ -87,12 +89,17 @@ const AudioFile: FC = () => {
8789

8890
return (
8991
<Container centered>
90-
<Button title={isPlaying ? 'Stop' : 'Play'} onPress={handlePress} />
91-
{!audioBuffer && <ActivityIndicator color="#FFFFFF" />}
92-
<Spacer.Vertical size={20} />
9392
<Button
9493
title="Set audio source from file"
9594
onPress={handleSetAudioSourceFromFile}
95+
width={200}
96+
/>
97+
{isLoading && <ActivityIndicator color="#FFFFFF" />}
98+
<Spacer.Vertical size={20} />
99+
<Button
100+
title={isPlaying ? 'Stop' : 'Play'}
101+
onPress={handlePress}
102+
disabled={!audioBuffer ? true : false}
96103
/>
97104
</Container>
98105
);

apps/common-app/src/examples/Piano/Piano.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FC, useEffect, useRef } from 'react';
22
import { AudioContext, AudioBuffer } from 'react-native-audio-api';
3+
import * as FileSystem from 'expo-file-system';
34

45
import { Container } from '../../components';
56
import { KeyName, sources, keyMap } from './utils';
@@ -27,8 +28,16 @@ const Piano: FC = () => {
2728
}
2829

2930
Object.entries(sources).forEach(async ([key, url]) => {
30-
bufferListRef.current[key as KeyName] =
31-
await audioContextRef.current!.decodeAudioDataSource(url);
31+
bufferListRef.current[key as KeyName] = await FileSystem.downloadAsync(
32+
url,
33+
FileSystem.documentDirectory + key.replace('#', 's') + '.mp3'
34+
)
35+
.then(({ uri }) => {
36+
return uri.replace('file://', '');
37+
})
38+
.then((uri) => {
39+
return audioContextRef.current!.decodeAudioDataSource(uri);
40+
});
3241
});
3342

3443
const newNotes: Partial<Record<KeyName, PianoNote>> = {};
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
module.exports = {
2-
presets: ['module:@react-native/babel-preset'],
3-
plugins: ['react-native-reanimated/plugin', 'module:react-native-dotenv'],
1+
module.exports = function (api) {
2+
api.cache(false);
3+
return {
4+
presets: ['module:@react-native/babel-preset'],
5+
plugins: ['react-native-reanimated/plugin', 'module:react-native-dotenv'],
6+
};
47
};

packages/react-native-audio-api/ios/AudioPlayer/AudioPlayer.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ - (int)getBufferSizeInFrames
6262
// which is safer to base our internal AudioBus sizes.
6363
// Buut no documentation => no guarantee :)
6464
// If something is crackling when it should play silence, start here 📻
65-
double maxBufferDuration = fmax(0.02, fmax(self.audioSession.IOBufferDuration, self.audioSession.preferredIOBufferDuration));
65+
double maxBufferDuration =
66+
fmax(0.02, fmax(self.audioSession.IOBufferDuration, self.audioSession.preferredIOBufferDuration));
6667
return (int)(maxBufferDuration * self.audioSession.sampleRate + 1);
6768
}
6869

0 commit comments

Comments
 (0)