feat: 倍速与字幕就近弹出菜单

This commit is contained in:
orz12
2024-03-01 06:32:19 +08:00
parent 05143fc90e
commit b7e2fd63e3
3 changed files with 72 additions and 132 deletions

View File

@@ -582,6 +582,13 @@ class VideoHttp {
SmartDialog.showToast("字幕${i['lan_doc']}加载失败, ${res.data['message']}"); SmartDialog.showToast("字幕${i['lan_doc']}加载失败, ${res.data['message']}");
} }
} }
if (subtitlesVtt.isNotEmpty) {
subtitlesVtt.insert(0, {
'language': '',
'title': '关闭字幕',
'text': ""
});
}
return subtitlesVtt; return subtitlesVtt;
} }
} }

View File

@@ -97,6 +97,7 @@ class PlPlayerController {
bool _enableHeart = true; bool _enableHeart = true;
bool _isFirstTime = true; bool _isFirstTime = true;
final RxList<Map<String, String>> _vttSubtitles = <Map<String, String>>[].obs; final RxList<Map<String, String>> _vttSubtitles = <Map<String, String>>[].obs;
final RxInt _vttSubtitlesIndex = 0.obs;
Timer? _timer; Timer? _timer;
Timer? _timerForSeek; Timer? _timerForSeek;
@@ -150,7 +151,8 @@ class PlPlayerController {
Stream<bool> get onMuteChanged => _mute.stream; Stream<bool> get onMuteChanged => _mute.stream;
// 视频字幕 // 视频字幕
RxList<dynamic> get vttSubtitles => _vttSubtitles; RxList<Map<String,String>> get vttSubtitles => _vttSubtitles;
RxInt get vttSubtitlesIndex => _vttSubtitlesIndex;
/// [videoPlayerController] instance of Player /// [videoPlayerController] instance of Player
Player? get videoPlayerController => _videoPlayerController; Player? get videoPlayerController => _videoPlayerController;
@@ -302,6 +304,7 @@ class PlPlayerController {
for (final PlaySpeed i in PlaySpeed.values) { for (final PlaySpeed i in PlaySpeed.values) {
speedsList.add(i.value); speedsList.add(i.value);
} }
speedsList.sort();
// _playerEventSubs = onPlayerStatusChanged.listen((PlayerStatus status) { // _playerEventSubs = onPlayerStatusChanged.listen((PlayerStatus status) {
// if (status == PlayerStatus.playing) { // if (status == PlayerStatus.playing) {
@@ -1106,62 +1109,18 @@ class PlPlayerController {
} }
} }
/// 选择字幕
void showSetSubtitleSheet() async {
showDialog(
context: Get.context!,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('选择字幕(测试)'),
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Wrap(
spacing: 8,
runSpacing: 2,
children: [
FilledButton.tonal(
onPressed: () async {
await removeSubtitle();
Get.back();
},
child: const Text("关闭字幕"),
),
for (final Map<String, String> i in _vttSubtitles) ...<Widget>[
FilledButton.tonal(
onPressed: () async {
await setSubtitle(i);
Get.back();
},
child: Text(i["title"]!),
),
]
],
);
}),
actions: <Widget>[
TextButton(
onPressed: () => Get.back(),
child: Text(
'取消',
style: TextStyle(color: Theme.of(context).colorScheme.outline),
),
),
],
);
},
);
}
removeSubtitle() {
_videoPlayerController?.setSubtitleTrack(SubtitleTrack.no());
}
// 设定字幕轨道 // 设定字幕轨道
setSubtitle(Map<String, String> s) { setSubtitle(Map<String, String> s) {
if (s['text'] == null) {
_videoPlayerController?.setSubtitleTrack(SubtitleTrack.no());
_vttSubtitlesIndex.value = 0;
return;
}
_videoPlayerController?.setSubtitleTrack(SubtitleTrack.data( _videoPlayerController?.setSubtitleTrack(SubtitleTrack.data(
s['text']!, s['text']!,
title: s['title']!, title: s['title']!,
language: s['language']!, language: s['language']!,
)); ));
_vttSubtitlesIndex.value = _vttSubtitles.indexOf(s);
} }
} }

View File

@@ -152,16 +152,35 @@ class BottomControl extends StatelessWidget implements PreferredSizeWidget {
: SizedBox( : SizedBox(
width: 45, width: 45,
height: 30, height: 30,
child: IconButton( child: PopupMenuButton<Map<String, String>>(
tooltip: '字幕', onSelected: (Map<String, String> value) {
style: ButtonStyle( controller!.setSubtitle(value);
padding: MaterialStateProperty.all(EdgeInsets.zero), },
), initialValue:
onPressed: () => _.showSetSubtitleSheet(), _.vttSubtitles[_.vttSubtitlesIndex.value],
icon: const Icon( color: Colors.black.withOpacity(0.8),
Icons.closed_caption_off_outlined, itemBuilder: (BuildContext context) {
size: 19, return _.vttSubtitles
color: Colors.white, .map((Map<String, String> subtitle) {
return PopupMenuItem<Map<String, String>>(
value: subtitle,
child: Text(
"${subtitle['title']}",
style: const TextStyle(color: Colors.white),
),
);
}).toList();
},
child: Container(
width: 45,
height: 30,
alignment: Alignment.center,
child: const Icon(
Icons.closed_caption_off_outlined,
size: 19,
color: Colors.white,
semanticLabel: '字幕',
),
), ),
), ),
), ),
@@ -169,17 +188,32 @@ class BottomControl extends StatelessWidget implements PreferredSizeWidget {
SizedBox( SizedBox(
width: 45, width: 45,
height: 30, height: 30,
child: TextButton( child: PopupMenuButton<double>(
style: ButtonStyle( onSelected: (double value) {
padding: MaterialStateProperty.all(EdgeInsets.zero), controller!.setPlaybackSpeed(value);
), },
onPressed: () => showSetSpeedSheet(), initialValue: _.playbackSpeed,
child: Obx( color: Colors.black.withOpacity(0.8),
() => Text( itemBuilder: (BuildContext context) {
'${_.playbackSpeed}X', return _.speedsList.map((double speed) {
style: const TextStyle(color: Colors.white, fontSize: 13), return PopupMenuItem<double>(
semanticsLabel: '${_.playbackSpeed}倍速', value: speed,
), child: Text(
"${speed}X",
style: const TextStyle(color: Colors.white),
semanticsLabel: "$speed倍速",
),
);
}).toList();
},
child: Container(
width: 45,
height: 30,
alignment: Alignment.center,
child: Obx(() => Text("${_.playbackSpeed}X",
style:
const TextStyle(color: Colors.white, fontSize: 13),
semanticsLabel: "${_.playbackSpeed}倍速")),
), ),
), ),
), ),
@@ -207,64 +241,4 @@ class BottomControl extends StatelessWidget implements PreferredSizeWidget {
), ),
); );
} }
/// 选择倍速
void showSetSpeedSheet() {
final double currentSpeed = controller!.playbackSpeed;
List<double> speedsList = controller!.speedsList;
showDialog(
context: Get.context!,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('播放速度'),
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Wrap(
spacing: 8,
runSpacing: 2,
children: [
for (final double i in speedsList) ...<Widget>[
if (i == currentSpeed) ...<Widget>[
FilledButton(
onPressed: () async {
// setState(() => currentSpeed = i),
await controller!.setPlaybackSpeed(i);
Get.back();
},
child: Text(i.toString()),
),
] else ...[
FilledButton.tonal(
onPressed: () async {
// setState(() => currentSpeed = i),
await controller!.setPlaybackSpeed(i);
Get.back();
},
child: Text(i.toString()),
),
]
]
],
);
}),
actions: <Widget>[
TextButton(
onPressed: () => Get.back(),
child: Text(
'取消',
style: TextStyle(color: Theme.of(context).colorScheme.outline),
),
),
TextButton(
onPressed: () async {
await controller!.setDefaultSpeed();
Get.back();
},
child: const Text('默认速度'),
),
],
);
},
);
}
} }