我正在使用expo播放音频记录,但是回调方法没有看到新分配的值。
一个按钮调用此方法:
1 // handles play/pause
2 const playAudio = async (audioRecording, audioRecordingTime, messageId) 3 => {
4 console.log('audioRecordingInState', audioRecordingInState);
5
6 try {
7 const soundObject = new Audio.Sound();
8 await soundObject.loadAsync({ uri: audioRecording });
9
10 soundObject.setOnPlaybackStatusUpdate(e => handlePlayBack(e));
11
12 const newStateSoundObject = {
13 soundObject,
14 messageId,
15 isPlaying: true
16 };
17
18 setAudioRecordingInState(newStateSoundObject);
19
20 await soundObject.playAsync();
21 } catch (err) {
22 console.log(err);
23 }
24 }
handlePlayback
是:
25 const handlePlayBack = async ({ didJustFinish }) => {
26 console.log("recording", audioRecordingInState);
27 if (didJustFinish) {
28 try {
29 setAudioRecordingInState(null);
30 } catch (err) {
31 console.log("err caught stopping", err);
32 }
33 }
34 };
每当单击该按钮时,都会播放音频并听到声音,但是第26行在应打印newStateSoundObject
时保持打印为空。我在这里做什么错了?
编辑:我已经测试了useEffect并以audioRecordingInState
作为依赖项,状态确实发生了变化,但是单击按钮播放音频,然后再次单击以暂停,则输出null(第4)。这是对第26行打印的null的补充。