mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 18:13:25 +00:00
feat: finish audiotrack switching
This commit is contained in:
45
apps/expo/src/stores/player/slices/audio.ts
Normal file
45
apps/expo/src/stores/player/slices/audio.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { Audio } from "expo-av";
|
||||
|
||||
import type { MakeSlice } from "./types";
|
||||
import type { AudioTrack } from "~/components/player/AudioTrackSelector";
|
||||
|
||||
export interface AudioSlice {
|
||||
audioObject: Audio.Sound | null;
|
||||
currentAudioTrack: AudioTrack | null;
|
||||
|
||||
setAudioObject(audioObject: Audio.Sound | null): void;
|
||||
setCurrentAudioTrack(track: AudioTrack | null): void;
|
||||
playAudio(): Promise<void>;
|
||||
pauseAudio(): Promise<void>;
|
||||
setAudioPositionAsync(positionMillis: number): Promise<void>;
|
||||
}
|
||||
|
||||
export const createAudioSlice: MakeSlice<AudioSlice> = (set, get) => ({
|
||||
audioObject: null,
|
||||
currentAudioTrack: null,
|
||||
|
||||
setAudioObject: (audioObject) => {
|
||||
set({ audioObject });
|
||||
},
|
||||
setCurrentAudioTrack: (track) => {
|
||||
set({ currentAudioTrack: track });
|
||||
},
|
||||
playAudio: async () => {
|
||||
const { audioObject } = get();
|
||||
if (audioObject) {
|
||||
await audioObject.playAsync();
|
||||
}
|
||||
},
|
||||
pauseAudio: async () => {
|
||||
const { audioObject } = get();
|
||||
if (audioObject) {
|
||||
await audioObject.pauseAsync();
|
||||
}
|
||||
},
|
||||
setAudioPositionAsync: async (positionMillis) => {
|
||||
const { audioObject } = get();
|
||||
if (audioObject) {
|
||||
await audioObject.setPositionAsync(positionMillis);
|
||||
}
|
||||
},
|
||||
});
|
@@ -1,9 +1,10 @@
|
||||
import type { StateCreator } from "zustand";
|
||||
|
||||
import type { AudioSlice } from "./audio";
|
||||
import type { InterfaceSlice } from "./interface";
|
||||
import type { VideoSlice } from "./video";
|
||||
|
||||
export type AllSlices = InterfaceSlice & VideoSlice;
|
||||
export type AllSlices = InterfaceSlice & VideoSlice & AudioSlice;
|
||||
|
||||
export type MakeSlice<Slice> = StateCreator<
|
||||
AllSlices,
|
||||
|
@@ -2,6 +2,7 @@ import { create } from "zustand";
|
||||
import { immer } from "zustand/middleware/immer";
|
||||
|
||||
import type { AllSlices } from "./slices/types";
|
||||
import { createAudioSlice } from "./slices/audio";
|
||||
import { createInterfaceSlice } from "./slices/interface";
|
||||
import { createVideoSlice } from "./slices/video";
|
||||
|
||||
@@ -9,5 +10,6 @@ export const usePlayerStore = create(
|
||||
immer<AllSlices>((...a) => ({
|
||||
...createInterfaceSlice(...a),
|
||||
...createVideoSlice(...a),
|
||||
...createAudioSlice(...a),
|
||||
})),
|
||||
);
|
||||
|
Reference in New Issue
Block a user