5
5
// - the code between BEGIN USER CODE and END USER CODE
6
6
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
7
7
// 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" ;
9
9
10
10
// BEGIN EXTRA CODE
11
11
// END EXTRA CODE
@@ -34,16 +34,7 @@ export async function PlaySound(audioFile?: mendix.lib.MxObject): Promise<void>
34
34
const changedDate = audioFile . get ( "changedDate" ) as number ;
35
35
const url = mx . data . getDocumentUrl ( guid , changedDate ) ;
36
36
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 ( ) => {
47
38
await TrackPlayer . reset ( ) ;
48
39
49
40
await TrackPlayer . add ( {
@@ -58,9 +49,15 @@ export async function PlaySound(audioFile?: mendix.lib.MxObject): Promise<void>
58
49
const listener = TrackPlayer . addEventListener ( Event . PlaybackQueueEnded , async ( ) => {
59
50
listener . remove ( ) ; // cleanup
60
51
} ) ;
52
+ } ;
53
+
54
+ try {
55
+ await play ( ) ;
61
56
} catch ( error ) {
57
+ // it means the player wasn't initialized yet.
58
+ await TrackPlayer . setupPlayer ( ) ;
59
+ await play ( ) ;
62
60
console . error ( "Playback failed" , error ) ;
63
- throw new Error ( "Playback failed due to an audio encoding error" ) ;
64
61
}
65
62
// END USER CODE
66
63
}
0 commit comments