Skip to content

Commit 2db0e6b

Browse files
committed
fix: audioFile app alignment
1 parent a443567 commit 2db0e6b

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect, useRef, useState, FC } from 'react';
1+
import React, { useCallback, useEffect, useRef, useState, useMemo, FC } from 'react';
22
import { ActivityIndicator } from 'react-native';
33
import {
44
AudioBuffer,
@@ -22,7 +22,6 @@ const AudioFile: FC = () => {
2222
const [isPlaying, setIsPlaying] = useState(false);
2323
const [isLoading, setIsLoading] = useState(false);
2424

25-
const [offset, setOffset] = useState(0);
2625
const [playbackRate, setPlaybackRate] = useState(INITIAL_RATE);
2726
const [elapsedTime, setElapsedTime] = useState(0);
2827

@@ -96,7 +95,7 @@ const AudioFile: FC = () => {
9695
const buffer = await fetch(URL)
9796
.then((response) => response.arrayBuffer())
9897
.then((arrayBuffer) =>
99-
audioContextRef.current!.decodeAudioData(arrayBuffer)
98+
audioContext.decodeAudioData(arrayBuffer)
10099
)
101100
.catch((error) => {
102101
console.error('Error decoding audio data source:', error);
@@ -109,8 +108,8 @@ const AudioFile: FC = () => {
109108
}, []);
110109

111110
useEffect(() => {
112-
if (!audioContextRef.current) {
113-
audioContextRef.current = new AudioContext();
111+
if (!audioBuffer) {
112+
fetchAudioBuffer();
114113
}
115114

116115
AudioManager.setLockScreenInfo({
@@ -120,18 +119,16 @@ const AudioFile: FC = () => {
120119
duration: TRACK_LENGTH,
121120
});
122121

122+
}, [fetchAudioBuffer])
123+
124+
useEffect(() => {
125+
123126
const remotePlaySubscription = AudioManager.enableSystemEvent(
124-
'remotePlay',
125-
(event) => {
126-
console.log('remotePlay event:', event);
127-
}
127+
'remotePlay', () => handlePress()
128128
);
129129

130130
const remotePauseSubscription = AudioManager.enableSystemEvent(
131-
'remotePause',
132-
(event) => {
133-
console.log('remotePause event:', event);
134-
}
131+
'remotePause', () => handlePress()
135132
);
136133

137134
const remoteChangePlaybackPositionSubscription =
@@ -157,25 +154,20 @@ const AudioFile: FC = () => {
157154
}
158155
);
159156

160-
AudioManager.observeAudioInterruptions(true);
161-
162-
fetchAudioBuffer();
163-
164157
return () => {
165158
remotePlaySubscription?.remove();
166159
remotePauseSubscription?.remove();
167160
remoteChangePlaybackPositionSubscription?.remove();
168161
interruptionSubscription?.remove();
169-
audioContextRef.current?.close();
170162
};
171-
}, [fetchAudioBuffer]);
163+
}, [handlePress]);
172164

173165
return (
174166
<Container centered>
175167
{isLoading && <ActivityIndicator color="#FFFFFF" />}
176168
<Button
177169
title={isPlaying ? 'Stop' : 'Play'}
178-
onPress={handlePress}
170+
onPress={() => handlePress()}
179171
disabled={!audioBuffer}
180172
/>
181173
<Spacer.Vertical size={49} />

0 commit comments

Comments
 (0)