feat(musik): tambahkan extensive debug logging untuk tracking seek issue
- Tambah key stabil pada audio element untuk mencegah remount - Log di seekTo: before/after currentTime - Log di onTimeUpdate: currentTime, rounded, isSeeking - Log di onChangeEnd slider: value, seekTime - Log di useEffect song change: currentSong, currentSongIndex - Log di skipBack/skipForward: index perubahan lagu Purpose: Track urutan eksekusi dan identifikasi race condition atau re-render yang tidak diinginkan Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -3,16 +3,23 @@ export function seekTo(
|
|||||||
time: number,
|
time: number,
|
||||||
setCurrentTime?: (v: number) => void
|
setCurrentTime?: (v: number) => void
|
||||||
) {
|
) {
|
||||||
if (!audioRef.current) return;
|
if (!audioRef.current) {
|
||||||
|
console.error('[seekTo] Audio element tidak ada!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[seekTo] Before seek - currentTime:', audioRef.current.currentTime, 'Target:', time);
|
||||||
|
|
||||||
// Set waktu audio
|
// Set waktu audio
|
||||||
audioRef.current.currentTime = time;
|
audioRef.current.currentTime = time;
|
||||||
|
|
||||||
|
console.log('[seekTo] After seek - currentTime:', audioRef.current.currentTime);
|
||||||
|
|
||||||
// Update state jika provided
|
// Update state jika provided
|
||||||
if (setCurrentTime) {
|
if (setCurrentTime) {
|
||||||
setCurrentTime(Math.round(time));
|
setCurrentTime(Math.round(time));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug log (bisa dihapus nanti)
|
// Debug log
|
||||||
console.log('[seekTo] Seek to:', time, 'seconds');
|
console.log('[seekTo] Seek to:', time, 'seconds');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ const MusicPlayer = () => {
|
|||||||
// Update duration when song changes
|
// Update duration when song changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (currentSong && audioRef.current) {
|
if (currentSong && audioRef.current) {
|
||||||
|
console.log('[useEffect Song Change] currentSong:', currentSong.judul, 'Index:', currentSongIndex);
|
||||||
// Gunakan durasi dari database sebagai acuan utama
|
// Gunakan durasi dari database sebagai acuan utama
|
||||||
const durationParts = currentSong.durasi.split(':');
|
const durationParts = currentSong.durasi.split(':');
|
||||||
const durationInSeconds = parseInt(durationParts[0]) * 60 + parseInt(durationParts[1]);
|
const durationInSeconds = parseInt(durationParts[0]) * 60 + parseInt(durationParts[1]);
|
||||||
@@ -161,6 +162,7 @@ const MusicPlayer = () => {
|
|||||||
const skipBack = () => {
|
const skipBack = () => {
|
||||||
const prevIndex = getPrevIndex(currentSongIndex, filteredMusik.length, isShuffle);
|
const prevIndex = getPrevIndex(currentSongIndex, filteredMusik.length, isShuffle);
|
||||||
if (prevIndex >= 0) {
|
if (prevIndex >= 0) {
|
||||||
|
console.log('[skipBack] Changing song to index:', prevIndex);
|
||||||
playSong(prevIndex);
|
playSong(prevIndex);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -168,6 +170,7 @@ const MusicPlayer = () => {
|
|||||||
const skipForward = () => {
|
const skipForward = () => {
|
||||||
const nextIndex = getNextIndex(currentSongIndex, filteredMusik.length, isShuffle);
|
const nextIndex = getNextIndex(currentSongIndex, filteredMusik.length, isShuffle);
|
||||||
if (nextIndex >= 0) {
|
if (nextIndex >= 0) {
|
||||||
|
console.log('[skipForward] Changing song to index:', nextIndex);
|
||||||
playSong(nextIndex);
|
playSong(nextIndex);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -193,13 +196,15 @@ const MusicPlayer = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Box px={{ base: 'md', md: 100 }} py="xl">
|
<Box px={{ base: 'md', md: 100 }} py="xl">
|
||||||
{/* Hidden audio element */}
|
{/* Hidden audio element - gunakan key yang stabil untuk mencegah remount */}
|
||||||
{currentSong?.audioFile && (
|
{currentSong?.audioFile && (
|
||||||
<audio
|
<audio
|
||||||
|
key={`audio-${currentSong.id}`}
|
||||||
ref={audioRef}
|
ref={audioRef}
|
||||||
src={currentSong.audioFile.link}
|
src={currentSong.audioFile.link}
|
||||||
muted={isMuted}
|
muted={isMuted}
|
||||||
onLoadedMetadata={() => {
|
onLoadedMetadata={(e) => {
|
||||||
|
console.log('[onLoadedMetadata] Audio loaded, duration:', e.currentTarget.duration);
|
||||||
// Jangan override duration dari database
|
// Jangan override duration dari database
|
||||||
// Audio element duration bisa berbeda beberapa ms
|
// Audio element duration bisa berbeda beberapa ms
|
||||||
if (audioRef.current) {
|
if (audioRef.current) {
|
||||||
@@ -211,10 +216,7 @@ const MusicPlayer = () => {
|
|||||||
// Gunakan pembulatan yang lebih smooth
|
// Gunakan pembulatan yang lebih smooth
|
||||||
const time = audioRef.current.currentTime;
|
const time = audioRef.current.currentTime;
|
||||||
const roundedTime = Math.round(time);
|
const roundedTime = Math.round(time);
|
||||||
// Debug: log jika ada perbedaan besar antara time dan state
|
console.log('[onTimeUpdate] currentTime:', time, 'rounded:', roundedTime, 'isSeeking:', isSeeking);
|
||||||
if (Math.abs(time - roundedTime) > 0.5) {
|
|
||||||
console.log('[onTimeUpdate] Time:', time, 'Rounded:', roundedTime);
|
|
||||||
}
|
|
||||||
setCurrentTime(roundedTime);
|
setCurrentTime(roundedTime);
|
||||||
}}
|
}}
|
||||||
onEnded={handleSongEnd}
|
onEnded={handleSongEnd}
|
||||||
@@ -279,11 +281,13 @@ const MusicPlayer = () => {
|
|||||||
setCurrentTime(v);
|
setCurrentTime(v);
|
||||||
}}
|
}}
|
||||||
onChangeEnd={(v) => {
|
onChangeEnd={(v) => {
|
||||||
setIsSeeking(false);
|
console.log('[Slider Atas onChangeEnd] START - value:', v);
|
||||||
// Validasi: jangan seek melebihi durasi
|
// Validasi: jangan seek melebihi durasi
|
||||||
const seekTime = Math.min(Math.max(0, v), duration);
|
const seekTime = Math.min(Math.max(0, v), duration);
|
||||||
|
// Set seeking false DULUAN sebelum seekTo
|
||||||
|
setIsSeeking(false);
|
||||||
|
console.log('[Slider Atas onChangeEnd] Calling seekTo with:', seekTime);
|
||||||
seekTo(audioRef, seekTime, setCurrentTime);
|
seekTo(audioRef, seekTime, setCurrentTime);
|
||||||
console.log('[Slider Atas] Seek end:', seekTime, 'Duration:', duration);
|
|
||||||
}}
|
}}
|
||||||
color="#0B4F78"
|
color="#0B4F78"
|
||||||
size="sm"
|
size="sm"
|
||||||
|
|||||||
Reference in New Issue
Block a user