全部包名与名称确定,后台播放能力已完整,准备进入媒体信息推送部分
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
|
||||
<application
|
||||
android:label="qt_player"
|
||||
android:label="清听"
|
||||
android:name="${applicationName}"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:usesCleartextTraffic="true"
|
||||
|
||||
+3
-3
@@ -259,9 +259,9 @@ class _QTPlayerAppState extends State<QTPlayerApp> {
|
||||
builder: () => _audioHandler!,
|
||||
config: const audio_service.AudioServiceConfig(
|
||||
androidNotificationChannelId: 'com.lxh.qingting_player.music',
|
||||
androidNotificationChannelName: '清听音乐播放',
|
||||
androidNotificationOngoing: true,
|
||||
androidStopForegroundOnPause: true,
|
||||
androidNotificationChannelName: '清听',
|
||||
androidNotificationOngoing: false,
|
||||
androidStopForegroundOnPause: false,
|
||||
),
|
||||
);
|
||||
debugPrint('✅ AudioService 初始化完成');
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
// lib/services/audio_player_handler.dart
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:audio_service/audio_service.dart' as audio_service;
|
||||
import '../audio/player_controller.dart';
|
||||
import '../audio/playback_state_manager.dart';
|
||||
import '../services/audio_service.dart'; // 你的业务 AudioService
|
||||
import '../services/audio_service.dart'; // 你自己的业务 AudioService
|
||||
|
||||
class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
final PlayerController _player = PlayerController();
|
||||
final PlaybackStateManager _state = PlaybackStateManager();
|
||||
|
||||
AudioPlayerHandler() {
|
||||
// ---- 监听播放状态变化,同步到通知栏 ----
|
||||
_player.playingStream.listen((playing) {
|
||||
_state.updatePlaying(playing);
|
||||
_publishState();
|
||||
@@ -32,22 +34,12 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
_publishState();
|
||||
});
|
||||
|
||||
_player.completedStream.listen((_) {
|
||||
debugPrint('⏭️ [handler] completed, auto next');
|
||||
AudioService().next();
|
||||
});
|
||||
// ⭐ 已删除 completed 监听,交由 AudioService 统一处理
|
||||
// _player.completedStream.listen((_) { ... });
|
||||
}
|
||||
|
||||
void _publishState() {
|
||||
final state = _state.playbackState;
|
||||
|
||||
debugPrint(
|
||||
'📡 [publish] '
|
||||
'playing=${state.playing}, '
|
||||
'controls=${state.controls.map((e) => e.action).toList()}',
|
||||
);
|
||||
|
||||
playbackState.add(state);
|
||||
playbackState.add(_state.playbackState);
|
||||
}
|
||||
|
||||
void updateNotification({
|
||||
@@ -64,17 +56,17 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
));
|
||||
}
|
||||
|
||||
// ---- 控制命令(系统回调) ----
|
||||
// ---- 系统控制命令 ----
|
||||
@override
|
||||
Future<void> play() async {
|
||||
debugPrint('▶️ [handler] play() called');
|
||||
await _player.play();
|
||||
_player.play();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> pause() async {
|
||||
debugPrint('⏸️ [handler] pause() called');
|
||||
await _player.pause();
|
||||
_player.pause();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -101,7 +93,6 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
AudioService().previous();
|
||||
}
|
||||
|
||||
// ⭐ 统一点击处理(仅使用 MediaButton 存在的枚举)
|
||||
@override
|
||||
Future<void> click(
|
||||
[audio_service.MediaButton button =
|
||||
@@ -115,7 +106,6 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
await skipToPrevious();
|
||||
break;
|
||||
case audio_service.MediaButton.media:
|
||||
// 播放/暂停切换
|
||||
if (_player.isPlaying) {
|
||||
await pause();
|
||||
} else {
|
||||
@@ -123,9 +113,12 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// 其他按钮(fastForward, rewind等)忽略或fallback
|
||||
debugPrint('⚠️ [handler] unhandled MediaButton: $button');
|
||||
break;
|
||||
// 其他按钮 fallback
|
||||
if (_player.isPlaying) {
|
||||
await pause();
|
||||
} else {
|
||||
await play();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,9 @@ class AudioService extends ChangeNotifier {
|
||||
bool _listening = false;
|
||||
final List<StreamSubscription> _subscriptions = [];
|
||||
|
||||
// ⭐ 防重入标志
|
||||
bool _handlingCompletion = false;
|
||||
|
||||
// ---- Getter(高频字段不走 ChangeNotifier) ----
|
||||
Song? get currentSong => _currentSong;
|
||||
bool get isPlaying => _isPlaying;
|
||||
@@ -240,12 +243,11 @@ class AudioService extends ChangeNotifier {
|
||||
player.stream.playing.listen((playing) {
|
||||
if (_isPlaying != playing) {
|
||||
_isPlaying = playing;
|
||||
notifyListeners(); // 只有播放状态变化才刷新
|
||||
notifyListeners();
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// ⭐ 进度更新:只更新 ValueNotifier,不触发全局重建
|
||||
_subscriptions.add(
|
||||
player.stream.position.listen((position) {
|
||||
positionNotifier.value = position;
|
||||
@@ -264,6 +266,7 @@ class AudioService extends ChangeNotifier {
|
||||
}),
|
||||
);
|
||||
|
||||
// ⭐ 唯一监听 completed 的地方
|
||||
_subscriptions.add(
|
||||
player.stream.completed.listen((_) {
|
||||
_onPlaybackCompleted();
|
||||
@@ -279,15 +282,23 @@ class AudioService extends ChangeNotifier {
|
||||
_subscriptions.clear();
|
||||
}
|
||||
|
||||
// ⭐ 防重入
|
||||
void _onPlaybackCompleted() {
|
||||
if (_queue.isEmpty) return;
|
||||
|
||||
if (_playMode == PlayMode.repeatOne) {
|
||||
_playCurrent();
|
||||
if (_handlingCompletion) {
|
||||
debugPrint('⚠️ [service] completed ignored: already handling');
|
||||
return;
|
||||
}
|
||||
|
||||
next();
|
||||
_handlingCompletion = true;
|
||||
try {
|
||||
if (_queue.isEmpty) return;
|
||||
if (_playMode == PlayMode.repeatOne) {
|
||||
_playCurrent();
|
||||
return;
|
||||
}
|
||||
next();
|
||||
} finally {
|
||||
_handlingCompletion = false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
name: qt_player
|
||||
name: qingting_player
|
||||
description: "清听 - 纯粹的自用音乐播放器"
|
||||
# The following line prevents the package from being accidentally published to
|
||||
# pub.dev using `flutter pub publish`. This is preferred for private packages.
|
||||
|
||||
Reference in New Issue
Block a user