Skip to content

Commit fccf0b6

Browse files
committed
🐛 fix: dance player error
1 parent a7646c2 commit fccf0b6

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/features/DanceList/Item/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const DanceItem = (props: DanceItemProps) => {
3333
s.setCurrentPlayId,
3434
]);
3535

36-
const [isPlaying, setIsPlaying] = useGlobalStore((s) => [s.isPlaying, s.setIsPlaying]);
36+
const [isPlaying, setIsPlaying] = useState(false);
3737

3838
const isCurrentPlay = currentPlayId ? currentPlayId === danceItem.danceId : false;
3939
const isSelected = currentIdentifier === danceItem.danceId;
@@ -45,7 +45,7 @@ const DanceItem = (props: DanceItemProps) => {
4545
const { downloading: danceDownloading, percent: dancePercent, fetchDanceUrl } = useLoadDance();
4646
const viewer = useGlobalStore((s) => s.viewer);
4747

48-
const handlePlayPause = () => {
48+
const handlePlayPause = async () => {
4949
viewer.model?.disposeAll();
5050
if (isPlaying && isCurrentPlay) {
5151
setIsPlaying(false);
@@ -55,11 +55,11 @@ const DanceItem = (props: DanceItemProps) => {
5555
setIsPlaying(true);
5656
const audioPromise = fetchAudioUrl(danceItem.danceId, danceItem.audio);
5757
const dancePromise = fetchDanceUrl(danceItem.danceId, danceItem.src);
58-
Promise.all([dancePromise, audioPromise]).then((res) => {
59-
if (!res) return;
60-
const [danceUrl, audioUrl] = res;
61-
if (danceUrl && audioUrl) viewer.model?.dance(danceUrl, audioUrl);
62-
});
58+
const [danceUrl, audioUrl] = await Promise.all([dancePromise, audioPromise]);
59+
if (danceUrl && audioUrl)
60+
viewer.model?.dance(danceUrl, audioUrl, () => {
61+
setIsPlaying(false);
62+
});
6363
}
6464
};
6565

src/features/audioPlayer/audioPlayer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class AudioPlayer {
88
}
99

1010
public async playFromArrayBuffer(buffer: ArrayBuffer, onEnded?: () => void) {
11+
this.stopPlay();
1112
const audioBuffer = await this.audio.decodeAudioData(buffer);
1213

1314
this.bufferSource = this.audio.createBufferSource();

src/libs/vrmViewer/model.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ export class Model {
3131
private _audioPlayer?: AudioPlayer;
3232
private _action: AnimationAction | undefined;
3333
private _clip: AnimationClip | undefined;
34-
private _audio: string | undefined;
3534

3635
constructor(lookAtTargetParent: THREE.Object3D) {
3736
this._lookAtTargetParent = lookAtTargetParent;
3837
this._lipSync = new LipSync(new AudioContext());
3938
this._audioPlayer = new AudioPlayer(new AudioContext());
4039
this._action = undefined;
4140
this._clip = undefined;
42-
this._audio = undefined;
4341
}
4442

4543
public async loadVRM(url: string): Promise<void> {
@@ -96,10 +94,7 @@ export class Model {
9694
this._action = undefined;
9795
}
9896

99-
if (this._audio) {
100-
this._audioPlayer?.stopPlay();
101-
this._audio = undefined;
102-
}
97+
this._audioPlayer?.stopPlay();
10398
}
10499

105100
/**
@@ -164,9 +159,9 @@ export class Model {
164159
const action = mixer.clipAction(clip);
165160
action.setLoop(LoopOnce, 1).play(); // play animation
166161
this._audioPlayer?.playFromURL(audioUrl, () => {
162+
this.disposeAll();
167163
onEnd?.();
168164
});
169-
this._audio = audioUrl;
170165

171166
this._action = action;
172167
this._clip = clip;

0 commit comments

Comments
 (0)