Skip to content

Commit

Permalink
♻️ 采用 media_kit 内置播放 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed May 1, 2024
1 parent ee76ca9 commit a7dc83d
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 227 deletions.
4 changes: 0 additions & 4 deletions lib/components/app/rss_dowload_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ class _RssDownloadCardState extends ConsumerState<RssDownloadCard> {
seeders = task.seederNumber;
all = task.allPeersNumber;
setState(() {});
if (progress == 100.toDouble()) {
if (mounted) await BtInfobar.success(context, '下载完成,即将删除任务');
await task.stop();
}
}

/// 开始下载
Expand Down
7 changes: 5 additions & 2 deletions lib/components/bangumi/subject_detail/bsd_bmf_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import 'package:filesize/filesize.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:media_kit/media_kit.dart';
import 'package:path/path.dart' as path;
import 'package:url_launcher/url_launcher_string.dart';

// Project imports:
import '../../../pages/bangumi/bangumi_play.dart';
import '../../../pages/app/play_page.dart';
import '../../../store/nav_store.dart';
import '../../../store/play_store.dart';
import '../../../tools/file_tool.dart';
import '../../../tools/log_tool.dart';
import '../../../tools/notifier_tool.dart';
Expand Down Expand Up @@ -281,10 +283,11 @@ class _BsdBmfFileInnerPlayerBtnState
onPressed: () async {
var navStore = ref.read(navStoreProvider);
var filePath = path.join(widget.download, widget.file);
ref.read(playStoreProvider.notifier).addTask(Media(filePath));
var pane = PaneItem(
icon: const Icon(FluentIcons.play),
title: const Text('内置播放'),
body: BangumiPlayPage(filePath),
body: const PlayPage(),
);
navStore.addNavItem(pane, '内置播放');
},
Expand Down
152 changes: 152 additions & 0 deletions lib/pages/app/play_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Package imports:
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';

// Project imports:
import '../../store/nav_store.dart';
import '../../store/play_store.dart';

/// 播放页面
class PlayPage extends ConsumerStatefulWidget {
/// 构造函数
const PlayPage({super.key});

@override
ConsumerState<PlayPage> createState() => _PlayPageState();
}

/// PlayPageState
class _PlayPageState extends ConsumerState<PlayPage>
with AutomaticKeepAliveClientMixin {
/// 播放器
late final player = Player();

/// 控制器
late final VideoController controller = VideoController(player);

/// 播放列表
List<Media> get list => ref.watch(playStoreProvider).list;

/// 保持状态
@override
bool get wantKeepAlive => true;

/// 初始化
@override
void initState() {
super.initState();
Future.microtask(() async {
await init();
});
}

/// dispose
@override
void dispose() {
Future.microtask(() async {
await player.dispose();
});
super.dispose();
}

/// init
Future<void> init() async {
var playlist = Playlist(list, index: list.length - 1);
await player.open(playlist);
}

/// 构建顶部栏
Widget buildHeader() {
return PageHeader(
leading: IconButton(
icon: const Icon(FluentIcons.back),
onPressed: () {
ref.read(navStoreProvider).removeNavItem('内置播放');
},
),
title: const Text('内置播放'),
);
}

/// 构建播放列表
Widget buildItemAct(Media item, int index) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(FluentIcons.play),
onPressed: () async {
await player.jump(index);
},
),
IconButton(
icon: const Icon(FluentIcons.delete),
onPressed: () async {
ref.read(playStoreProvider.notifier).removeTask(item);
await player.remove(index);
},
),
],
);
}

/// 构建播放列表
Widget buildList() {
return SizedBox(
width: 200,
height: MediaQuery.of(context).size.height,
child: Card(
child: ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
var item = list[index];
var name = Uri.parse(item.uri).pathSegments.last;
return ListTile(
title: Tooltip(
message: name,
child: Text(
name,
maxLines: 4,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
),
trailing: buildItemAct(item, index),
);
},
),
),
);
}

/// 构建
@override
Widget build(BuildContext context) {
super.build(context);
if (list.isEmpty) {
return ScaffoldPage(
header: buildHeader(),
content: const Center(child: Text('没有找到任何播放任务')),
);
}
return ScaffoldPage(
header: buildHeader(),
content: Padding(
padding: const EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(child: Video(controller: controller)),
const SizedBox(width: 8),
buildList(),
],
),
),
);
}
}
131 changes: 0 additions & 131 deletions lib/pages/bangumi/bangumi_play.dart

This file was deleted.

36 changes: 36 additions & 0 deletions lib/store/play_store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Package imports:
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:media_kit/media_kit.dart';

/// 播放列表提供者
final playStoreProvider = ChangeNotifierProvider<BtPlayStore>((ref) {
return BtPlayStore();
});

/// 播放列表
class BtPlayStore extends ChangeNotifier {
/// 播放列表
final List<Media> _list = [];

/// 获取播放列表
List<Media> get list => _list;

/// 添加播放任务
bool addTask(Media media) {
/// 判断是否已经存在
var find = _list.indexWhere((e) => e == media);
if (find != -1) {
return false;
}
_list.add(media);
notifyListeners();
return true;
}

/// 移除播放任务
void removeTask(Media media) {
_list.removeWhere((e) => e == media);
notifyListeners();
}
}
6 changes: 4 additions & 2 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import media_kit_libs_macos_video
import media_kit_video
import package_info_plus
import path_provider_foundation
import screen_brightness_macos
import screen_retriever
import sqflite
import system_theme
import url_launcher_macos
import video_player_avfoundation
import wakelock_plus
import window_manager

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
Expand All @@ -33,10 +34,11 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
MediaKitVideoPlugin.register(with: registry.registrar(forPlugin: "MediaKitVideoPlugin"))
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
ScreenBrightnessMacosPlugin.register(with: registry.registrar(forPlugin: "ScreenBrightnessMacosPlugin"))
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
SystemThemePlugin.register(with: registry.registrar(forPlugin: "SystemThemePlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
FVPVideoPlayerPlugin.register(with: registry.registrar(forPlugin: "FVPVideoPlayerPlugin"))
WakelockPlusMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockPlusMacosPlugin"))
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
}
Loading

0 comments on commit a7dc83d

Please sign in to comment.