|
| 1 | +--- |
| 2 | +title: Music Player Now Available! |
| 3 | +date: "2025-06-14" |
| 4 | +description: "Been wrestling with bugs non-stop (´・ω・`)" |
| 5 | +--- |
| 6 | + |
| 7 | +# Section of Music |
| 8 | + |
| 9 | +Hi there! This is The Infinity's! |
| 10 | + |
| 11 | +I create music as a hobby, and today I'm excited to announce that I've built a dedicated page to share my music with you all! |
| 12 | + |
| 13 | +## Key Features |
| 14 | + |
| 15 | +Our music player comes with the following features: |
| 16 | + |
| 17 | +1. Basic Playback Controls |
| 18 | + - Play/Pause |
| 19 | + - Track seeking |
| 20 | + - Fast forward/Rewind |
| 21 | + - Next/Previous track navigation |
| 22 | + |
| 23 | +2. Playback Speed Control |
| 24 | + - Adjustable from 0.5x to 2x speed |
| 25 | + |
| 26 | +3. Equalizer Function |
| 27 | + - 5-band equalizer |
| 28 | + - Multiple presets |
| 29 | + - Custom settings available |
| 30 | + |
| 31 | +4. Visualizer |
| 32 | + - Toggle between linear and circular display |
| 33 | + - Real-time audio waveform visualization |
| 34 | + |
| 35 | +## Technical Implementation Details |
| 36 | + |
| 37 | +### Audio Processing Implementation |
| 38 | + |
| 39 | +```tsx |
| 40 | +const audioCtxRef = useRef<AudioContext | null>(null); |
| 41 | +const elementSource = audioCtxRef.current.createMediaElementSource( |
| 42 | + audioRef.current |
| 43 | +); |
| 44 | +``` |
| 45 | + |
| 46 | +We're utilizing the Web Audio API to create an audio context, which enables us to process and analyze audio signals. |
| 47 | + |
| 48 | +### Equalizer Implementation |
| 49 | + |
| 50 | +```tsx |
| 51 | +const eqFilters: BiquadFilterNode[] = [ |
| 52 | + audioCtxRef.current.createBiquadFilter(), |
| 53 | + // ... |
| 54 | +]; |
| 55 | + |
| 56 | +const frequencies = [60, 250, 1000, 4000, 16000]; |
| 57 | +``` |
| 58 | + |
| 59 | +The equalizer employs BiquadFilterNodes for each of the five frequency bands (from bass to treble). |
| 60 | +Each filter is assigned a specific center frequency and can be adjusted using the gain parameter. |
| 61 | + |
| 62 | +### Visualizer Rendering |
| 63 | + |
| 64 | +```tsx |
| 65 | +const renderFrame = () => { |
| 66 | + analyserNode.getByteFrequencyData(dataArray); |
| 67 | + // ... |
| 68 | + animationFrameRef.current = requestAnimationFrame(renderFrame); |
| 69 | +}; |
| 70 | +``` |
| 71 | + |
| 72 | +We're using an AnalyserNode to analyze audio data in real-time and rendering the waveform using the Canvas API. |
| 73 | +The requestAnimationFrame ensures smooth animation performance. |
| 74 | + |
| 75 | +### Playback Control Implementation |
| 76 | + |
| 77 | +```tsx |
| 78 | +const handleTogglePlay = () => { |
| 79 | + if (!audioRef.current || !audioCtxRef.current) return; |
| 80 | + |
| 81 | + if (audioCtxRef.current.state === "suspended") { |
| 82 | + audioCtxRef.current.resume(); |
| 83 | + setPlayState("play"); |
| 84 | + } |
| 85 | + // ... |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +The playback control combines HTMLAudioElement and AudioContext to manage audio playback. |
| 90 | +State management is handled appropriately based on user interactions. |
| 91 | + |
| 92 | +### Playlist Functionality |
| 93 | + |
| 94 | +```tsx |
| 95 | +const handleNextTrack = () => { |
| 96 | + if (!musicList || musicList.length === 0) return; |
| 97 | + const nextId = getNextTrackId(); |
| 98 | + window.location.href = `/music/${nextId}`; |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +The playlist system determines the next or previous track based on the current track index |
| 103 | +and handles navigation to the appropriate URL. |
| 104 | + |
| 105 | +## Conclusion |
| 106 | + |
| 107 | +By combining the Web Audio API and Canvas API, we've successfully created a full-featured music player in the browser. |
| 108 | +While implementing the equalizer and visualizer presented some challenges, |
| 109 | +we're pleased with the result - a user-friendly player with advanced audio playback capabilities! |
| 110 | + |
| 111 | +While there's always room for improvement, we're happy to have completed these core features. |
| 112 | +In future updates, we plan to add more user-friendly features like playlist saving and shuffle play functionality! |
0 commit comments