@umituz/react-native-sound 1.2.9 → 1.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -4
- package/src/AudioManager.ts +18 -14
- package/src/types.ts +17 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-sound",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.11",
|
|
4
4
|
"description": "Universal sound playback and caching library for React Native apps",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -32,12 +32,14 @@
|
|
|
32
32
|
"react-native": ">=0.74.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
+
"@react-native-async-storage/async-storage": "^2.2.0",
|
|
36
|
+
"@types/react": "~19.1.10",
|
|
35
37
|
"@umituz/react-native-storage": "latest",
|
|
36
38
|
"expo-av": "~13.4.1",
|
|
37
|
-
"@types/react": "~19.1.10",
|
|
38
39
|
"react": "19.1.0",
|
|
39
40
|
"react-native": "0.81.5",
|
|
40
|
-
"typescript": "~5.9.2"
|
|
41
|
+
"typescript": "~5.9.2",
|
|
42
|
+
"zustand": "^5.0.9"
|
|
41
43
|
},
|
|
42
44
|
"publishConfig": {
|
|
43
45
|
"access": "public"
|
|
@@ -47,4 +49,4 @@
|
|
|
47
49
|
"README.md",
|
|
48
50
|
"LICENSE"
|
|
49
51
|
]
|
|
50
|
-
}
|
|
52
|
+
}
|
package/src/AudioManager.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Audio, AVPlaybackStatus,
|
|
1
|
+
import { Audio, AVPlaybackStatus, InterruptionModeIOS, InterruptionModeAndroid } from 'expo-av';
|
|
2
2
|
import { useSoundStore } from './store';
|
|
3
|
-
import { PlaybackOptions, SoundSource } from './types';
|
|
3
|
+
import { PlaybackOptions, SoundSource, isPlaybackStatusSuccess, isSoundSourceValid } from './types';
|
|
4
4
|
|
|
5
5
|
class AudioManager {
|
|
6
6
|
private sound: Audio.Sound | null = null;
|
|
@@ -28,7 +28,7 @@ class AudioManager {
|
|
|
28
28
|
private onPlaybackStatusUpdate = (status: AVPlaybackStatus) => {
|
|
29
29
|
const store = useSoundStore.getState();
|
|
30
30
|
|
|
31
|
-
if (!status
|
|
31
|
+
if (!isPlaybackStatusSuccess(status)) {
|
|
32
32
|
if (status.error) {
|
|
33
33
|
store.setError(status.error);
|
|
34
34
|
store.setPlaying(false);
|
|
@@ -36,14 +36,13 @@ class AudioManager {
|
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
store.
|
|
41
|
-
store.
|
|
42
|
-
store.setProgress(s.positionMillis, s.durationMillis || 0);
|
|
39
|
+
store.setPlaying(status.isPlaying);
|
|
40
|
+
store.setBuffering(status.isBuffering);
|
|
41
|
+
store.setProgress(status.positionMillis, status.durationMillis || 0);
|
|
43
42
|
|
|
44
|
-
if (
|
|
43
|
+
if (status.didJustFinish && !status.isLooping) {
|
|
45
44
|
store.setPlaying(false);
|
|
46
|
-
store.setProgress(
|
|
45
|
+
store.setProgress(status.durationMillis || 0, status.durationMillis || 0);
|
|
47
46
|
}
|
|
48
47
|
};
|
|
49
48
|
|
|
@@ -52,7 +51,12 @@ class AudioManager {
|
|
|
52
51
|
|
|
53
52
|
if (__DEV__) console.log('[AudioManager] Play called with ID:', id);
|
|
54
53
|
|
|
55
|
-
|
|
54
|
+
if (!isSoundSourceValid(source)) {
|
|
55
|
+
const errorMsg = 'Invalid sound source: source is null or undefined';
|
|
56
|
+
store.setError(errorMsg);
|
|
57
|
+
throw new Error(errorMsg);
|
|
58
|
+
}
|
|
59
|
+
|
|
56
60
|
if (this.currentId === id && this.sound) {
|
|
57
61
|
const status = await this.sound.getStatusAsync();
|
|
58
62
|
if (status.isLoaded) {
|
|
@@ -67,7 +71,6 @@ class AudioManager {
|
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
try {
|
|
70
|
-
// Unload previous sound
|
|
71
74
|
await this.unload();
|
|
72
75
|
|
|
73
76
|
this.currentId = id;
|
|
@@ -77,7 +80,7 @@ class AudioManager {
|
|
|
77
80
|
if (__DEV__) console.log('[AudioManager] Creating sound from source:', source);
|
|
78
81
|
|
|
79
82
|
const { sound } = await Audio.Sound.createAsync(
|
|
80
|
-
source
|
|
83
|
+
source,
|
|
81
84
|
{
|
|
82
85
|
shouldPlay: true,
|
|
83
86
|
isLooping: options?.isLooping ?? false,
|
|
@@ -90,9 +93,10 @@ class AudioManager {
|
|
|
90
93
|
|
|
91
94
|
this.sound = sound;
|
|
92
95
|
if (__DEV__) console.log('[AudioManager] Sound created and playing');
|
|
93
|
-
} catch (error
|
|
96
|
+
} catch (error) {
|
|
97
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
|
|
94
98
|
if (__DEV__) console.error('[AudioManager] Error playing sound:', error);
|
|
95
|
-
store.setError(
|
|
99
|
+
store.setError(errorMessage);
|
|
96
100
|
this.currentId = null;
|
|
97
101
|
throw error;
|
|
98
102
|
}
|
package/src/types.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @umituz/react-native-sound Types
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { AVPlaybackStatus } from 'expo-av';
|
|
5
|
+
import { AVPlaybackStatus, AVPlaybackStatusSuccess } from 'expo-av';
|
|
6
6
|
|
|
7
7
|
export type SoundSource = number | { uri: string; headers?: Record<string, string> } | null;
|
|
8
8
|
|
|
@@ -27,3 +27,19 @@ export interface SoundState {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export type PlaybackStatus = AVPlaybackStatus;
|
|
30
|
+
|
|
31
|
+
export interface SoundError {
|
|
32
|
+
message: string;
|
|
33
|
+
code?: string;
|
|
34
|
+
originalError?: unknown;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type PlaybackStatusSuccess = AVPlaybackStatusSuccess;
|
|
38
|
+
|
|
39
|
+
export function isPlaybackStatusSuccess(status: AVPlaybackStatus): status is PlaybackStatusSuccess {
|
|
40
|
+
return status.isLoaded;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function isSoundSourceValid(source: SoundSource): source is number | { uri: string; headers?: Record<string, string> } {
|
|
44
|
+
return source !== null && source !== undefined;
|
|
45
|
+
}
|