add check for songs when incrementing index

This commit is contained in:
jeffvli
2021-08-16 05:47:53 -07:00
parent 677e2b9d31
commit 6dca2dad0c
2 changed files with 49 additions and 39 deletions

View File

@@ -20,21 +20,23 @@ const Player = ({ children }: any) => {
const playQueue = useAppSelector((state) => state.playQueue);
useEffect(() => {
if (playQueue.status === 'PAUSED') {
player1Ref.current.audioEl.current.pause();
player2Ref.current.audioEl.current.pause();
} else if (playQueue.currentPlayer === 1) {
try {
player1Ref.current.audioEl.current.play();
} catch (err) {
console.log(err);
if (playQueue.status === 'PLAYING') {
if (playQueue.currentPlayer === 1) {
try {
player1Ref.current.audioEl.current.play();
} catch (err) {
console.log(err);
}
} else {
try {
player2Ref.current.audioEl.current.play();
} catch (err) {
console.log(err);
}
}
} else {
try {
player2Ref.current.audioEl.current.play();
} catch (err) {
console.log(err);
}
player1Ref.current.audioEl.current.pause();
player2Ref.current.audioEl.current.pause();
}
}, [playQueue.currentPlayer, playQueue.status]);

View File

@@ -84,26 +84,28 @@ const playQueueSlice = createSlice({
},
incrementCurrentIndex: (state, action: PayloadAction<string>) => {
if (state.currentIndex < state.entry.length - 1) {
state.currentIndex += 1;
if (action.payload === 'usingHotkey') {
state.currentPlayer = 1;
state.player1.volume = state.volume;
state.player1.index = state.currentIndex;
state.player2.index = state.currentIndex + 1;
if (state.entry.length >= 1) {
if (state.currentIndex < state.entry.length - 1) {
state.currentIndex += 1;
if (action.payload === 'usingHotkey') {
state.currentPlayer = 1;
state.player1.volume = state.volume;
state.player1.index = state.currentIndex;
state.player2.index = state.currentIndex + 1;
}
}
}
if (state.repeatAll) {
state.currentIndex = 0;
if (action.payload === 'usingHotkey') {
state.currentPlayer = 1;
state.player1.index = 0;
state.player2.index = 1;
if (state.repeatAll) {
state.currentIndex = 0;
if (action.payload === 'usingHotkey') {
state.currentPlayer = 1;
state.player1.index = 0;
state.player2.index = 1;
}
}
}
state.currentSongId = state.entry[state.currentIndex].id;
state.currentSongId = state.entry[state.currentIndex].id;
}
},
incrementPlayerIndex: (state, action: PayloadAction<number>) => {
@@ -121,9 +123,11 @@ const playQueueSlice = createSlice({
(track) => track.id === action.payload.id
);
state.currentPlayer = 1;
state.player1.index = findIndex;
state.player1.volume = state.volume;
state.player2.index = findIndex + 1;
state.player2.volume = 0;
state.currentPlayer = 1;
state.currentIndex = findIndex;
state.currentSongId = action.payload.id;
},
@@ -140,16 +144,18 @@ const playQueueSlice = createSlice({
},
decrementCurrentIndex: (state, action: PayloadAction<string>) => {
if (state.currentIndex > 0) {
state.currentIndex -= 1;
if (action.payload === 'usingHotkey') {
state.currentPlayer = 1;
state.player1.index = state.currentIndex;
state.player2.index = state.currentIndex + 1;
if (state.entry.length >= 1) {
if (state.currentIndex > 0) {
state.currentIndex -= 1;
if (action.payload === 'usingHotkey') {
state.currentPlayer = 1;
state.player1.index = state.currentIndex;
state.player2.index = state.currentIndex + 1;
}
}
}
state.currentSongId = state.entry[state.currentIndex].id;
state.currentSongId = state.entry[state.currentIndex].id;
}
},
setCurrentIndex: (state, action: PayloadAction<Entry>) => {
@@ -162,7 +168,9 @@ const playQueueSlice = createSlice({
},
setPlayQueue: (state, action: PayloadAction<Entry[]>) => {
state.status = 'PLAYING';
if (state.status !== 'PLAYING') {
state.status = 'PLAYING';
}
state.currentIndex = 0;
state.currentSongId = action.payload[0].id;
action.payload.map((entry: any) => state.entry.push(entry));