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