Skip to content

Commit d156952

Browse files
committed
♻️ Drop asset_audio_player's implementation.
1 parent 568fac6 commit d156952

File tree

2 files changed

+69
-61
lines changed

2 files changed

+69
-61
lines changed

lib/src/widget/builder/audio_page_builder.dart

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'dart:async';
66

77
import 'package:flutter/material.dart';
8-
import 'package:assets_audio_player/assets_audio_player.dart';
8+
import 'package:video_player/video_player.dart';
99

1010
import 'package:wechat_assets_picker/src/constants/constants.dart';
1111
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
@@ -22,34 +22,39 @@ class AudioPageBuilder extends StatefulWidget {
2222
final AssetEntity asset;
2323

2424
/// [State] for asset picker viewer.
25-
/// 资源查看器的状态[State]
25+
/// 资源查看器的状态 [State]
2626
final AssetPickerViewerState state;
2727

2828
@override
2929
State<StatefulWidget> createState() => _AudioPageBuilderState();
3030
}
3131

3232
class _AudioPageBuilderState extends State<AudioPageBuilder> {
33-
/// Create an [AssetsAudioPlayer] instance for the page builder state.
34-
/// 创建一个[AssetsAudioPlayer]的实例
35-
final AssetsAudioPlayer audioPlayer = AssetsAudioPlayer();
33+
/// A [StreamController] for current position of the [_controller].
34+
/// 控制器当前的播放进度
35+
final StreamController<Duration> durationStreamController =
36+
StreamController<Duration>.broadcast();
37+
38+
/// Create a [VideoPlayerController] instance for the page builder state.
39+
/// 创建一个 [VideoPlayerController] 的实例
40+
VideoPlayerController _controller;
3641

3742
/// Whether the audio loaded.
3843
/// 音频是否已经加载完成
3944
bool isLoaded = false;
4045

41-
/// Whether there's some error when loading the audio.
42-
/// 加载音频时是否有错误
43-
bool isError = false;
46+
/// Whether the player is playing.
47+
/// 播放器是否在播放
48+
bool isPlaying = false;
49+
50+
/// Whether the controller is playing.
51+
/// 播放控制器是否在播放
52+
bool get isControllerPlaying => _controller?.value?.isPlaying ?? false;
4453

4554
/// Duration of the audio.
4655
/// 音频的时长
4756
Duration assetDuration;
4857

49-
/// Audio instance.
50-
/// 音频实例
51-
Audio audio;
52-
5358
@override
5459
void initState() {
5560
super.initState();
@@ -61,22 +66,23 @@ class _AudioPageBuilderState extends State<AudioPageBuilder> {
6166
/// Stop and dispose player instance to stop playing
6267
/// when dispose (e.g. page switched).
6368
/// 状态销毁时停止并销毁实例(例如页面切换时)
64-
audioPlayer
65-
..stop()
66-
..dispose();
69+
_controller?.pause();
70+
_controller?.removeListener(audioPlayerListener);
71+
_controller?.dispose();
6772
super.dispose();
6873
}
6974

70-
/// Using [audioPlayer] to load content url from the asset.
71-
/// 使用 [audioPlayer] 通过content地址加载资源
75+
/// Load content url from the asset.
76+
/// 通过content地址加载资源
7277
Future<void> openAudioFile() async {
7378
try {
74-
assetDuration = Duration(seconds: widget.asset.duration);
75-
audio = Audio.file(await widget.asset.getMediaUrl());
76-
audioPlayer.open(audio, autoStart: false);
79+
final String url = await widget.asset.getMediaUrl();
80+
assetDuration = widget.asset.duration.seconds;
81+
_controller = VideoPlayerController.network(url);
82+
await _controller.initialize();
83+
_controller.addListener(audioPlayerListener);
7784
} catch (e) {
7885
realDebugPrint('Error when opening audio file: $e');
79-
isError = true;
8086
} finally {
8187
isLoaded = true;
8288
if (mounted) {
@@ -85,6 +91,22 @@ class _AudioPageBuilderState extends State<AudioPageBuilder> {
8591
}
8692
}
8793

94+
/// Listener for the player.
95+
/// 播放器的监听方法
96+
void audioPlayerListener() {
97+
if (isControllerPlaying != isPlaying) {
98+
isPlaying = isControllerPlaying;
99+
if (mounted) {
100+
setState(() {});
101+
}
102+
}
103+
104+
/// Add the current position into the stream.
105+
if (_controller?.value?.position != null) {
106+
durationStreamController.add(_controller.value.position);
107+
}
108+
}
109+
88110
/// Title widget.
89111
/// 标题组件
90112
Widget get titleWidget => Text(
@@ -97,41 +119,32 @@ class _AudioPageBuilderState extends State<AudioPageBuilder> {
97119

98120
/// Button to control audio play/pause.
99121
/// 控制音频播放或暂停的按钮
100-
Widget get audioControlButton => StreamBuilder<bool>(
101-
initialData: false,
102-
stream: audioPlayer.isPlaying,
103-
builder: (BuildContext _, AsyncSnapshot<bool> data) {
104-
final bool isPlaying = data.data;
105-
return GestureDetector(
106-
onTap: () {
107-
if (isPlaying) {
108-
audioPlayer.pause();
109-
} else {
110-
audioPlayer.play();
111-
}
112-
},
113-
child: Container(
114-
margin: const EdgeInsets.all(20.0),
115-
decoration: BoxDecoration(
116-
boxShadow: <BoxShadow>[BoxShadow(color: Colors.black12)],
117-
shape: BoxShape.circle,
118-
),
119-
child: Icon(
120-
isPlaying
121-
? Icons.pause_circle_outline
122-
: Icons.play_circle_filled,
123-
size: 70.0,
124-
),
125-
),
126-
);
122+
Widget get audioControlButton => GestureDetector(
123+
onTap: () {
124+
if (isPlaying) {
125+
_controller.pause();
126+
} else {
127+
_controller.play();
128+
}
127129
},
130+
child: Container(
131+
margin: const EdgeInsets.all(20.0),
132+
decoration: BoxDecoration(
133+
boxShadow: <BoxShadow>[BoxShadow(color: Colors.black12)],
134+
shape: BoxShape.circle,
135+
),
136+
child: Icon(
137+
isPlaying ? Icons.pause_circle_outline : Icons.play_circle_filled,
138+
size: 70.0,
139+
),
140+
),
128141
);
129142

130143
/// Duration indicator for the audio.
131144
/// 音频的时长指示器
132145
Widget get durationIndicator => StreamBuilder<Duration>(
133146
initialData: Duration.zero,
134-
stream: audioPlayer.currentPosition,
147+
stream: durationStreamController.stream,
135148
builder: (BuildContext _, AsyncSnapshot<Duration> data) {
136149
return Text(
137150
'${Constants.textDelegate.durationIndicatorBuilder(data.data)}'
@@ -150,17 +163,13 @@ class _AudioPageBuilderState extends State<AudioPageBuilder> {
150163
return ColoredBox(
151164
color: context.themeData.backgroundColor,
152165
child: isLoaded
153-
? AudioWidget(
154-
audio: audio,
155-
play: audioPlayer.isPlaying.value,
156-
child: Column(
157-
mainAxisAlignment: MainAxisAlignment.center,
158-
children: <Widget>[
159-
titleWidget,
160-
audioControlButton,
161-
durationIndicator,
162-
],
163-
),
166+
? Column(
167+
mainAxisAlignment: MainAxisAlignment.center,
168+
children: <Widget>[
169+
titleWidget,
170+
audioControlButton,
171+
durationIndicator,
172+
],
164173
)
165174
: const SizedBox.shrink(),
166175
);

pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ dependencies:
1111
flutter:
1212
sdk: flutter
1313

14-
assets_audio_player: ^2.0.7+9
1514
camera: ^0.5.8+2
1615
extended_image: ^0.9.0
1716
flutter_common_exports: ^0.1.0

0 commit comments

Comments
 (0)