9.8 KiB
Phase B: Queue + Playback Integration Guide
Estimated Integration Time: 20-30 minutes
Overview
Phase B adds full audio playback with queue management to the Feishin shell. Users can now:
- Click play on any track
- Navigate queue with next/previous
- Control volume, seek, repeat mode
- See now-playing info in footer
Step 1: Copy Phase B Component Files
Copy these 4 new files to your project:
# Services
cp QueueContext.tsx kmfrontend/src/frontend/services/
cp PlaybackService.ts kmfrontend/src/frontend/services/
# Components
cp AudioPlayer.tsx kmfrontend/src/frontend/components/
cp AudioPlayer.scss kmfrontend/src/frontend/components/
File locations:
kmfrontend/src/frontend/
├── services/
│ ├── SubsonicContext.tsx (Phase A)
│ ├── SubsonicAPI.ts (Phase A)
│ ├── QueueContext.tsx (Phase B) ← NEW
│ └── PlaybackService.ts (Phase B) ← NEW
├── components/
│ ├── FeishinShell.tsx (Phase A)
│ ├── SubsonicBrowser.tsx (Phase A, modified)
│ ├── SubsonicSetup.tsx (Phase A)
│ ├── LibraryPage.tsx (Phase A, modified)
│ ├── AudioPlayer.tsx (Phase B) ← NEW
│ └── AudioPlayer.scss (Phase B) ← NEW
└── hooks/
├── useSubsonic.ts (Phase A)
Step 2: Update LibraryPage.tsx
Update kmfrontend/src/frontend/components/LibraryPage.tsx:
Change 1: Add imports
import AudioPlayer from '../AudioPlayer';
import { QueueProvider, useQueue } from '../services/QueueContext';
Change 2: Create inner component
Move existing component logic to LibraryPageInner and wrap with QueueProvider:
Before:
function LibraryPage() {
const subsonic = useSubsonic();
// ... existing code
}
After:
function LibraryPageInner() {
const subsonic = useSubsonic();
const queue = useQueue();
const renderContent = () => {
if (!subsonic.status.connected) {
return <SubsonicSetup />;
}
return <SubsonicBrowser />;
};
return (
<>
<AudioPlayer />
<FeishinShell
nowPlayingTrack={queue.currentTrack || undefined}
isPlaying={queue.playbackState.isPlaying}
currentTime={queue.playbackState.currentTime}
duration={queue.playbackState.duration}
onPlayPause={queue.togglePlayPause}
onNext={queue.nextTrack}
onPrevious={queue.previousTrack}
onSeek={queue.seek}
>
<div style={{ width: '100%', height: '100%', overflow: 'hidden' }}>
<Routes>
<Route path="/home" element={renderContent()} />
<Route path="/albums" element={renderContent()} />
<Route path="/tracks" element={renderContent()} />
<Route path="/albumartists" element={renderContent()} />
<Route path="/artists" element={renderContent()} />
<Route path="/genres" element={renderContent()} />
<Route path="/playlists" element={renderContent()} />
<Route path="/sing" element={<div>Sing browser (Phase C)</div>} />
<Route path="*" element={<Navigate to="/library/home" />} />
</Routes>
</div>
</FeishinShell>
</>
);
}
function LibraryPage() {
return (
<QueueProvider>
<LibraryPageInner />
</QueueProvider>
);
}
export default LibraryPage;
Step 3: Update SubsonicBrowser.tsx
Update kmfrontend/src/frontend/components/SubsonicBrowser.tsx:
Change 1: Add import
import { useQueue } from '../services/QueueContext';
Change 2: Add queue hook to component
export default function SubsonicBrowser() {
const { connection, status } = useSubsonic();
const { addMultipleToQueue, setQueue, playTrackAtIndex } = useQueue();
const [api, setApi] = useState<SubsonicAPI | null>(null);
// ... rest of code
Change 3: Add play handler methods
const handlePlayTracks = (tracksToPlay: SubsonicTrack[], startIndex = 0) => {
setQueue(tracksToPlay);
playTrackAtIndex(startIndex);
};
const handleAddToQueue = (track: SubsonicTrack) => {
addMultipleToQueue([track]);
};
Change 4: Update track rendering
Add play button to track items:
Before:
{tracks.map((track, index) => (
<div key={track.id} className="track-item">
<div className="track-number">{index + 1}</div>
<div className="track-info">
<div className="track-title">{track.title}</div>
<div className="track-artist">{track.artist}</div>
</div>
<div className="track-album">{track.album}</div>
<div className="track-duration">{Math.floor((track.duration || 0) / 60)}:00</div>
</div>
))}
After:
{tracks.map((track, index) => (
<div key={track.id} className="track-item">
<button
className="track-play-btn"
onClick={() => handlePlayTracks(tracks, index)}
title="Play"
>
▶
</button>
<div className="track-number">{index + 1}</div>
<div className="track-info">
<div className="track-title">{track.title}</div>
<div className="track-artist">{track.artist}</div>
</div>
<div className="track-album">{track.album}</div>
<div className="track-duration">{Math.floor((track.duration || 0) / 60)}:00</div>
</div>
))}
Also update album detail tracks:
{currentAlbum.tracks.map((track, index) => (
<div key={track.id} className="track-item">
<button
className="track-play-btn"
onClick={() => handlePlayTracks(currentAlbum.tracks, index)}
title="Play"
>
▶
</button>
<div className="track-number">{index + 1}</div>
<div className="track-title">{track.title}</div>
<div className="track-duration">{Math.floor((track.duration || 0) / 60)}:00</div>
</div>
))}
Step 4: Update SubsonicBrowser.scss (Optional)
Add styling for play buttons:
.track-play-btn {
background: none;
border: none;
color: #1db954;
cursor: pointer;
font-size: 12px;
padding: 4px 8px;
border-radius: 4px;
transition: background 0.2s;
&:hover {
background: rgba(29, 185, 84, 0.2);
color: #1ed760;
}
}
.track-item {
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border-radius: 4px;
transition: background 0.2s;
&:hover {
background: rgba(255, 255, 255, 0.05);
}
}
Step 5: Build and Test
cd kmfrontend
# Install dependencies (if needed)
npm install --legacy-peer-deps
# Build
npm run build
# Or run dev server
npm run dev
Quick Test Checklist
- No TypeScript errors after build
- App starts without errors
- Navigate to library/albums
- Click play button on track
- Audio plays (check browser console)
- Footer shows current track
- Play/pause button works
- Next/previous buttons work
- Seek bar works
- Volume slider works
Troubleshooting
Error: "Cannot find module 'QueueContext'"
- Verify file copied to
kmfrontend/src/frontend/services/QueueContext.tsx - Check import path:
../services/QueueContext
Error: "useQueue must be used within QueueProvider"
- Ensure LibraryPageInner is wrapped with
<QueueProvider> - Verify component hierarchy: LibraryPage → QueueProvider → LibraryPageInner
No audio plays when clicking play button
- Check browser console for errors
- Verify Subsonic connection is active
- Check network tab: /rest/stream.view requests
- Verify auth params in stream URL (username/password)
Play button doesn't appear
- Verify SubsonicBrowser.tsx updated with play button render
- Check browser dev tools: button element present?
- Verify CSS class
track-play-btnhas styles
Progress bar not updating
- Check AudioPlayer
onTimeUpdateevent firing - Verify
setCurrentTimeis being called - Check FeishinShell receives
currentTimeprop
API Integration Details
Subsonic Stream URL
Format:
/rest/stream.view?id=<trackId>&u=<username>&p=<password>&c=<clientName>&v=1.12.0
Example:
http://subsonic.example.com:4040/rest/stream.view?id=12345&u=admin&p=password&c=stemengine&v=1.12.0
Requirements:
id: Track ID from Subsonicu: Username (from SubsonicConnection)p: Password ortfor token (from SubsonicConnection)c: Client name (from SubsonicConnection.clientName)v: API version (1.12.0)
CORS Headers
AudioPlayer uses crossOrigin="anonymous":
<audio
crossOrigin="anonymous"
onTimeUpdate={handleTimeUpdate}
// ...
/>
This tells browser to make CORS request. Subsonic must allow it.
Error Handling
If stream fails:
- AudioPlayer
onErrorevent fires - Calls
setError(error.message) - Stored in QueueContext.playbackState.error
- Display in UI (or log to console)
Performance Notes
- Memory: Each track loaded into state (not streamed)
- Network: Stream URL fetched per track (lazy loaded)
- Audio: HTML5 native playback (efficient)
- State: React Context (simple, adequate for now)
Next Steps: Phase C
After Phase B is integrated, proceed to Phase C:
- Create Sing browser at
/singroute - Add convert button to detail pages
- Integrate AutoPipeline for track processing
- Show conversion queue and progress
Reference: KARAOKE_FRONTEND_STEMENGINE_CONNECTIVITY_PLAN.md Section 21
Questions & Support
See PHASE_A_INDEX.md for documentation index.
For Phase B specific questions, check:
- PHASE_B_SUMMARY.md — What was delivered
- KARAOKE_FRONTEND_STEMENGINE_CONNECTIVITY_PLAN.md Section 21 — Architecture details
Status: Phase B ✅ Ready to Integrate
Estimated Duration: 20-30 minutes