播放页部分调整,播放链路已打通
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// lib/services/audio_service.dart
|
||||
import 'dart:async'; // ⬅️ 添加这个导入
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'playback_service.dart';
|
||||
|
||||
@@ -22,6 +22,7 @@ class AudioService extends ChangeNotifier {
|
||||
bool _isPlaying = false;
|
||||
Duration _position = Duration.zero;
|
||||
Duration _duration = Duration.zero;
|
||||
Duration _bufferedPosition = Duration.zero; // ✅ 新增:缓冲位置
|
||||
|
||||
bool _listening = false;
|
||||
final List<StreamSubscription> _subscriptions = [];
|
||||
@@ -30,12 +31,13 @@ class AudioService extends ChangeNotifier {
|
||||
bool get isPlaying => _isPlaying;
|
||||
Duration get position => _position;
|
||||
Duration get duration => _duration;
|
||||
Duration get bufferedPosition => _bufferedPosition; // ✅ 新增 getter
|
||||
|
||||
// ✅ 完整的播放入口
|
||||
Future<void> playSong(Song song) async {
|
||||
_currentSong = song;
|
||||
_position = Duration.zero;
|
||||
_duration = Duration.zero;
|
||||
_bufferedPosition = Duration.zero;
|
||||
|
||||
_startListening();
|
||||
notifyListeners();
|
||||
@@ -44,11 +46,9 @@ class AudioService extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
|
||||
// 播放由 PlaybackService 执行,状态由 stream 更新
|
||||
await PlaybackService().play(song.url!);
|
||||
}
|
||||
|
||||
// ✅ 只调用播放器方法,不手动修改 _isPlaying
|
||||
void togglePlay() {
|
||||
if (_currentSong == null) return;
|
||||
|
||||
@@ -57,7 +57,6 @@ class AudioService extends ChangeNotifier {
|
||||
} else {
|
||||
PlaybackService().resume();
|
||||
}
|
||||
// _isPlaying 由 player.stream.playing 更新
|
||||
}
|
||||
|
||||
void stopPlay() {
|
||||
@@ -65,6 +64,7 @@ class AudioService extends ChangeNotifier {
|
||||
_isPlaying = false;
|
||||
_position = Duration.zero;
|
||||
_duration = Duration.zero;
|
||||
_bufferedPosition = Duration.zero;
|
||||
_stopListening();
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -75,7 +75,6 @@ class AudioService extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// ✅ 直接监听 media_kit 的三个独立 Stream
|
||||
void _startListening() {
|
||||
if (_listening) return;
|
||||
_listening = true;
|
||||
@@ -108,6 +107,16 @@ class AudioService extends ChangeNotifier {
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// ✅ 新增:监听缓冲位置
|
||||
_subscriptions.add(
|
||||
player.stream.buffer.listen((buffer) {
|
||||
if (_bufferedPosition != buffer) {
|
||||
_bufferedPosition = buffer;
|
||||
notifyListeners();
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
void _stopListening() {
|
||||
|
||||
Reference in New Issue
Block a user