added audio output for demo songs since spotify api doesnt support audio streaming

This commit is contained in:
Leon Astner 2025-08-02 05:41:50 +02:00
parent 6b93f1206d
commit a91654df03
14 changed files with 1261 additions and 77 deletions

View file

@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import '../models/spotify_track.dart';
import '../services/spotify_service.dart';
import '../services/audio_service.dart';
class QueueItem {
final SpotifyTrack track;
@ -33,11 +34,10 @@ class MusicQueueService extends ChangeNotifier {
MusicQueueService._internal();
final SpotifyService _spotifyService = SpotifyService();
final AudioService _audioService = AudioService();
// Current playing track
SpotifyTrack? _currentTrack;
bool _isPlaying = false;
double _progress = 0.0;
// Queue management
final List<QueueItem> _queue = [];
@ -46,9 +46,9 @@ class MusicQueueService extends ChangeNotifier {
final List<SpotifyTrack> _recentlyPlayed = [];
// Getters
SpotifyTrack? get currentTrack => _currentTrack;
bool get isPlaying => _isPlaying;
double get progress => _progress;
SpotifyTrack? get currentTrack => _audioService.currentTrack ?? _currentTrack;
bool get isPlaying => _audioService.isPlaying;
double get progress => _audioService.progress;
List<QueueItem> get queue => List.unmodifiable(_queue);
List<SpotifyTrack> get recentlyPlayed => List.unmodifiable(_recentlyPlayed);
@ -102,13 +102,14 @@ class MusicQueueService extends ChangeNotifier {
});
}
// Playback simulation
void playNext() {
// Playback control
Future<void> playNext() async {
if (_queue.isNotEmpty) {
final nextItem = _queue.removeAt(0);
_currentTrack = nextItem.track;
_isPlaying = true;
_progress = 0.0;
// Use audio service to actually play the track
await _audioService.playTrack(nextItem.track);
// Add to recently played
_recentlyPlayed.insert(0, nextItem.track);
@ -118,37 +119,25 @@ class MusicQueueService extends ChangeNotifier {
notifyListeners();
print('Now playing: ${_currentTrack!.name} by ${_currentTrack!.artistNames}');
// Simulate track progress
_simulatePlayback();
}
}
void togglePlayPause() {
_isPlaying = !_isPlaying;
Future<void> togglePlayPause() async {
await _audioService.togglePlayPause();
notifyListeners();
}
void skipTrack() {
playNext();
Future<void> skipTrack() async {
await playNext();
}
void _simulatePlayback() {
if (_currentTrack == null) return;
// Simulate track progress over time
Future.delayed(const Duration(seconds: 1), () {
if (_isPlaying && _currentTrack != null) {
_progress += 1.0 / (_currentTrack!.durationMs / 1000);
if (_progress >= 1.0) {
// Track finished, play next
playNext();
} else {
notifyListeners();
_simulatePlayback();
}
}
});
Future<void> seekTo(double position) async {
if (_audioService.totalDuration != Duration.zero) {
final seekPosition = Duration(
milliseconds: (position * _audioService.totalDuration.inMilliseconds).round(),
);
await _audioService.seekTo(seekPosition);
}
}
// Initialize with some popular tracks
@ -165,9 +154,14 @@ class MusicQueueService extends ChangeNotifier {
_queue.add(queueItem);
}
// Set up audio service callback for track completion
_audioService.onTrackComplete = () {
playNext();
};
// Start playing the first track
if (_queue.isNotEmpty) {
playNext();
await playNext();
}
notifyListeners();
@ -189,16 +183,18 @@ class MusicQueueService extends ChangeNotifier {
// Get current track info for display
Map<String, dynamic>? get currentTrackInfo {
if (_currentTrack == null) return null;
final track = currentTrack;
if (track == null) return null;
return {
'title': _currentTrack!.name,
'artist': _currentTrack!.artistNames,
'album': _currentTrack!.album.name,
'imageUrl': _currentTrack!.imageUrl,
'duration': _currentTrack!.duration,
'progress': _progress,
'isPlaying': _isPlaying,
'title': track.name,
'artist': track.artistNames,
'album': track.album.name,
'imageUrl': track.imageUrl,
'duration': _audioService.totalDurationString,
'currentTime': _audioService.currentPositionString,
'progress': progress,
'isPlaying': isPlaying,
};
}
}