Skip to content

Commit 8c29984

Browse files
author
Harun.Karahan
committed
initialization issue was fixed for track player
1 parent b42f43c commit 8c29984

File tree

1 file changed

+9
-12
lines changed
  • packages/jsActions/mobile-resources-native/src/platform

1 file changed

+9
-12
lines changed

packages/jsActions/mobile-resources-native/src/platform/PlaySound.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// - the code between BEGIN USER CODE and END USER CODE
66
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
77
// Other code you write will be lost the next time you deploy the project.
8-
import TrackPlayer, { Event, State } from "react-native-track-player";
8+
import TrackPlayer, { Event } from "react-native-track-player";
99

1010
// BEGIN EXTRA CODE
1111
// END EXTRA CODE
@@ -34,16 +34,7 @@ export async function PlaySound(audioFile?: mendix.lib.MxObject): Promise<void>
3434
const changedDate = audioFile.get("changedDate") as number;
3535
const url = mx.data.getDocumentUrl(guid, changedDate);
3636

37-
const isPlayerInitialized = async () => {
38-
const currentTrack = await TrackPlayer.getActiveTrack();
39-
return currentTrack !== null;
40-
};
41-
42-
try {
43-
if (!isPlayerInitialized()) {
44-
await TrackPlayer.setupPlayer();
45-
}
46-
37+
const play = async () => {
4738
await TrackPlayer.reset();
4839

4940
await TrackPlayer.add({
@@ -58,9 +49,15 @@ export async function PlaySound(audioFile?: mendix.lib.MxObject): Promise<void>
5849
const listener = TrackPlayer.addEventListener(Event.PlaybackQueueEnded, async () => {
5950
listener.remove(); // cleanup
6051
});
52+
};
53+
54+
try {
55+
await play();
6156
} catch (error) {
57+
// it means the player wasn't initialized yet.
58+
await TrackPlayer.setupPlayer();
59+
await play();
6260
console.error("Playback failed", error);
63-
throw new Error("Playback failed due to an audio encoding error");
6461
}
6562
// END USER CODE
6663
}

0 commit comments

Comments
 (0)