feat: 新增媒体通知
This commit is contained in:
@ -16,6 +16,7 @@ import 'package:pilipala/pages/search/index.dart';
|
||||
import 'package:pilipala/pages/video/detail/index.dart';
|
||||
import 'package:pilipala/router/app_pages.dart';
|
||||
import 'package:pilipala/pages/main/view.dart';
|
||||
import 'package:pilipala/services/service_locator.dart';
|
||||
import 'package:pilipala/utils/app_scheme.dart';
|
||||
import 'package:pilipala/utils/data.dart';
|
||||
import 'package:pilipala/utils/storage.dart';
|
||||
@ -28,6 +29,7 @@ void main() async {
|
||||
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
|
||||
.then((_) async {
|
||||
await GStrorage.init();
|
||||
await setupServiceLocator();
|
||||
runApp(const MyApp());
|
||||
// 小白条、导航栏沉浸
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
||||
|
||||
@ -12,6 +12,7 @@ import 'package:pilipala/common/widgets/stat/view.dart';
|
||||
import 'package:pilipala/models/video_detail_res.dart';
|
||||
import 'package:pilipala/pages/video/detail/introduction/controller.dart';
|
||||
import 'package:pilipala/pages/video/detail/widgets/ai_detail.dart';
|
||||
import 'package:pilipala/services/service_locator.dart';
|
||||
import 'package:pilipala/utils/feed_back.dart';
|
||||
import 'package:pilipala/utils/storage.dart';
|
||||
import 'package:pilipala/utils/utils.dart';
|
||||
@ -51,12 +52,14 @@ class _VideoIntroPanelState extends State<VideoIntroPanel>
|
||||
_futureBuilderFuture = videoIntroController.queryVideoIntro();
|
||||
videoIntroController.videoDetail.listen((value) {
|
||||
videoDetail = value;
|
||||
videoPlayerServiceHandler.onVideoIntroChange(value);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
videoIntroController.onClose();
|
||||
videoPlayerServiceHandler.onVideoIntroDispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:easy_debounce/easy_throttle.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:flutter_volume_controller/flutter_volume_controller.dart';
|
||||
@ -14,6 +15,7 @@ import 'package:ns_danmaku/ns_danmaku.dart';
|
||||
import 'package:pilipala/http/video.dart';
|
||||
import 'package:pilipala/plugin/pl_player/index.dart';
|
||||
import 'package:pilipala/plugin/pl_player/models/play_repeat.dart';
|
||||
import 'package:pilipala/services/service_locator.dart';
|
||||
import 'package:pilipala/utils/feed_back.dart';
|
||||
import 'package:pilipala/utils/storage.dart';
|
||||
import 'package:screen_brightness/screen_brightness.dart';
|
||||
@ -526,12 +528,24 @@ class PlPlayerController {
|
||||
}),
|
||||
videoPlayerController!.stream.buffering.listen((event) {
|
||||
isBuffering.value = event;
|
||||
videoPlayerServiceHandler.onStatusChange(
|
||||
playerStatus.status.value, event);
|
||||
}),
|
||||
// videoPlayerController!.stream.volume.listen((event) {
|
||||
// if (!mute.value && _volumeBeforeMute != event) {
|
||||
// _volumeBeforeMute = event / 100;
|
||||
// }
|
||||
// }),
|
||||
// 媒体通知监听
|
||||
onPlayerStatusChanged.listen((event) {
|
||||
videoPlayerServiceHandler.onStatusChange(event, isBuffering.value);
|
||||
}),
|
||||
onPositionChanged.listen((event) {
|
||||
EasyThrottle.throttle(
|
||||
'mediaServicePositon',
|
||||
const Duration(seconds: 1),
|
||||
() => videoPlayerServiceHandler.onPositionChange(event));
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
116
lib/services/audio_handler.dart
Normal file
116
lib/services/audio_handler.dart
Normal file
@ -0,0 +1,116 @@
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:pilipala/models/video_detail_res.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:pilipala/plugin/pl_player/index.dart';
|
||||
|
||||
Future<VideoPlayerServiceHandler> initAudioService() async {
|
||||
return await AudioService.init(
|
||||
builder: () => VideoPlayerServiceHandler(),
|
||||
config: const AudioServiceConfig(
|
||||
androidNotificationChannelId: 'com.guozhigq.pilipala.audio',
|
||||
androidNotificationChannelName: 'Audio Service Pilipala',
|
||||
androidNotificationOngoing: true,
|
||||
androidStopForegroundOnPause: true,
|
||||
fastForwardInterval: Duration(seconds: 10),
|
||||
rewindInterval: Duration(seconds: 10),
|
||||
androidNotificationChannelDescription: 'Media notification channel',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class VideoPlayerServiceHandler extends BaseAudioHandler
|
||||
with QueueHandler, SeekHandler {
|
||||
static final List<MediaItem> _item = [];
|
||||
|
||||
@override
|
||||
Future<void> play() async {
|
||||
PlPlayerController.getInstance().play();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> pause() async {
|
||||
PlPlayerController.getInstance().pause();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> seek(Duration position) async {
|
||||
playbackState.add(playbackState.value.copyWith(
|
||||
updatePosition: position,
|
||||
));
|
||||
await PlPlayerController.getInstance().seekTo(position);
|
||||
}
|
||||
|
||||
Future<void> setMediaItem(MediaItem newMediaItem) async {
|
||||
mediaItem.add(newMediaItem);
|
||||
addQueueItem(newMediaItem);
|
||||
}
|
||||
|
||||
Future<void> setPlaybackState(PlayerStatus status, bool isBuffering) async {
|
||||
final AudioProcessingState processingState;
|
||||
final playing = status == PlayerStatus.playing;
|
||||
if (status == PlayerStatus.completed) {
|
||||
processingState = AudioProcessingState.completed;
|
||||
} else if (isBuffering) {
|
||||
processingState = AudioProcessingState.buffering;
|
||||
} else {
|
||||
processingState = AudioProcessingState.ready;
|
||||
}
|
||||
|
||||
playbackState.add(playbackState.value.copyWith(
|
||||
processingState:
|
||||
isBuffering ? AudioProcessingState.buffering : processingState,
|
||||
controls: [
|
||||
MediaControl.rewind.copyWith(androidIcon: 'drawable/ic_stat_replay_10'),
|
||||
if (playing) MediaControl.pause else MediaControl.play,
|
||||
MediaControl.fastForward
|
||||
.copyWith(androidIcon: 'drawable/ic_stat_forward_10'),
|
||||
],
|
||||
playing: playing,
|
||||
systemActions: const {
|
||||
MediaAction.seek,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
onStatusChange(PlayerStatus status, bool isBuffering) {
|
||||
if (_item.isEmpty) return;
|
||||
setPlaybackState(status, isBuffering);
|
||||
}
|
||||
|
||||
onVideoIntroChange(VideoDetailData data) {
|
||||
Map argMap = Get.arguments;
|
||||
final heroTag = argMap['heroTag'];
|
||||
|
||||
final mediaItem = MediaItem(
|
||||
id: heroTag,
|
||||
title: data.title ?? "",
|
||||
artist: data.owner?.name ?? "",
|
||||
duration: Duration(seconds: data.duration ?? 0),
|
||||
artUri: Uri.parse(data.pic ?? ""),
|
||||
);
|
||||
setMediaItem(mediaItem);
|
||||
_item.add(mediaItem);
|
||||
}
|
||||
|
||||
onVideoIntroDispose() {
|
||||
playbackState.add(playbackState.value.copyWith(
|
||||
processingState: AudioProcessingState.idle,
|
||||
playing: false,
|
||||
));
|
||||
stop();
|
||||
_item.removeLast();
|
||||
if (_item.isNotEmpty) {
|
||||
setMediaItem(_item.last);
|
||||
}
|
||||
if (_item.isEmpty) {
|
||||
playbackState
|
||||
.add(playbackState.value.copyWith(updatePosition: Duration.zero));
|
||||
}
|
||||
}
|
||||
|
||||
onPositionChange(Duration position) {
|
||||
playbackState.add(playbackState.value.copyWith(
|
||||
updatePosition: position,
|
||||
));
|
||||
}
|
||||
}
|
||||
8
lib/services/service_locator.dart
Normal file
8
lib/services/service_locator.dart
Normal file
@ -0,0 +1,8 @@
|
||||
import 'audio_handler.dart';
|
||||
|
||||
late VideoPlayerServiceHandler videoPlayerServiceHandler;
|
||||
|
||||
Future<void> setupServiceLocator() async {
|
||||
final audio = await initAudioService();
|
||||
videoPlayerServiceHandler = audio;
|
||||
}
|
||||
Reference in New Issue
Block a user