Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ethan
/
appframe
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit 6dfc85c0
authored
2025-12-10 22:16:19 +0800
by
tanghuan
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
音频播放接口增加播放速率控制,保存文件接口让用户选择保存路径
1 parent
751e9aa5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
6 deletions
lib/config/locator.dart
lib/data/repositories/message/audio_player_handler.dart
lib/data/repositories/message/save_file_to_disk_handler.dart
lib/config/locator.dart
View file @
6dfc85c
...
@@ -156,6 +156,7 @@ Future<void> setupLocator() async {
...
@@ -156,6 +156,7 @@ Future<void> setupLocator() async {
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioPauseHandler
(),
instanceName:
'audioPause'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioPauseHandler
(),
instanceName:
'audioPause'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioResumeHandler
(),
instanceName:
'audioResume'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioResumeHandler
(),
instanceName:
'audioResume'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioSeekHandler
(),
instanceName:
'audioSeek'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioSeekHandler
(),
instanceName:
'audioSeek'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioPlayRateHandler
(),
instanceName:
'audioPlayRate'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioStopHandler
(),
instanceName:
'audioStop'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioStopHandler
(),
instanceName:
'audioStop'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioClearHandler
(),
instanceName:
'audioClear'
);
getIt
.
registerLazySingleton
<
MessageHandler
>(()
=>
AudioClearHandler
(),
instanceName:
'audioClear'
);
...
...
lib/data/repositories/message/audio_player_handler.dart
View file @
6dfc85c
...
@@ -14,13 +14,15 @@ class AudioPlayHandler extends MessageHandler {
...
@@ -14,13 +14,15 @@ class AudioPlayHandler extends MessageHandler {
final
seek
=
params
[
'seek'
]
as
int
?
??
0
;
final
seek
=
params
[
'seek'
]
as
int
?
??
0
;
// 暂时忽略
// 暂时忽略
// final isBg = params['isBg'] as bool ?? false;
// final isBg = params['isBg'] as bool ?? false;
final
playRate
=
(
params
[
'playRate'
]
as
num
?)?.
toDouble
()
??
1.0
;
var
playId
=
params
[
'playId'
]
as
String
?
??
''
;
var
playId
=
params
[
'playId'
]
as
String
?
??
''
;
if
(
playId
.
isEmpty
)
{
if
(
playId
.
isEmpty
)
{
playId
=
Uuid
().
v4
();
playId
=
Uuid
().
v4
();
}
}
var
playerService
=
getIt
.
get
<
PlayerService
>();
var
playerService
=
getIt
.
get
<
PlayerService
>();
var
result
=
await
playerService
.
playAudio
(
url
,
seek
,
playId
);
var
result
=
await
playerService
.
playAudio
(
url
,
seek
,
playId
,
playRate
);
if
(!
result
)
{
if
(!
result
)
{
throw
Exception
(
'播放错误'
);
throw
Exception
(
'播放错误'
);
...
@@ -39,7 +41,8 @@ class AudioPauseHandler extends MessageHandler {
...
@@ -39,7 +41,8 @@ class AudioPauseHandler extends MessageHandler {
class
AudioResumeHandler
extends
MessageHandler
{
class
AudioResumeHandler
extends
MessageHandler
{
@override
@override
Future
<
dynamic
>
handleMessage
(
dynamic
params
)
async
{
Future
<
dynamic
>
handleMessage
(
dynamic
params
)
async
{
return
await
getIt
.
get
<
PlayerService
>().
resumeAudio
();
final
playRate
=
(
params
[
'playRate'
]
as
num
?)?.
toDouble
()
??
1.0
;
return
await
getIt
.
get
<
PlayerService
>().
resumeAudio
(
playRate
);
}
}
}
}
...
@@ -55,6 +58,20 @@ class AudioSeekHandler extends MessageHandler {
...
@@ -55,6 +58,20 @@ class AudioSeekHandler extends MessageHandler {
}
}
}
}
class
AudioPlayRateHandler
extends
MessageHandler
{
@override
Future
<
dynamic
>
handleMessage
(
dynamic
params
)
async
{
if
(
params
is
!
Map
<
String
,
dynamic
>)
{
throw
Exception
(
'参数错误'
);
}
// var playId = params['playId'] as String? ?? '';
final
playRate
=
(
params
[
'playRate'
]
as
num
?)?.
toDouble
()
??
1.0
;
return
await
getIt
.
get
<
PlayerService
>().
rateAudio
(
playRate
);
}
}
class
AudioStopHandler
extends
MessageHandler
{
class
AudioStopHandler
extends
MessageHandler
{
@override
@override
Future
<
dynamic
>
handleMessage
(
dynamic
params
)
async
{
Future
<
dynamic
>
handleMessage
(
dynamic
params
)
async
{
...
...
lib/data/repositories/message/save_file_to_disk_handler.dart
View file @
6dfc85c
...
@@ -2,8 +2,8 @@ import 'dart:io';
...
@@ -2,8 +2,8 @@ import 'dart:io';
import
'package:appframe/services/dispatcher.dart'
;
import
'package:appframe/services/dispatcher.dart'
;
import
'package:dio/dio.dart'
;
import
'package:dio/dio.dart'
;
import
'package:file_picker/file_picker.dart'
;
import
'package:path/path.dart'
as
path
;
import
'package:path/path.dart'
as
path
;
import
'package:path_provider/path_provider.dart'
;
class
SaveFileToDisKHandler
extends
MessageHandler
{
class
SaveFileToDisKHandler
extends
MessageHandler
{
@override
@override
...
@@ -17,9 +17,19 @@ class SaveFileToDisKHandler extends MessageHandler {
...
@@ -17,9 +17,19 @@ class SaveFileToDisKHandler extends MessageHandler {
throw
Exception
(
'参数错误'
);
throw
Exception
(
'参数错误'
);
}
}
String
ext
=
path
.
extension
(
filePath
);
// 让用户选择保存目录
final
Directory
tempDir
=
await
getApplicationDocumentsDirectory
();
String
?
selectedDirectory
=
await
FilePicker
.
platform
.
getDirectoryPath
();
final
targetPath
=
path
.
join
(
tempDir
.
path
,
'
${DateTime.now().millisecondsSinceEpoch}$ext
'
);
// 获取filePath中的文件名,以及后缀名
var
fileName
=
path
.
basenameWithoutExtension
(
filePath
);
var
ext
=
path
.
extension
(
filePath
);
// 要保存的路径
var
targetPath
=
path
.
join
(
selectedDirectory
!,
'
$fileName
.
$ext
'
);
// 如果文件存在,则在文件名后添加数字
int
i
=
1
;
while
(
File
(
targetPath
).
existsSync
())
{
targetPath
=
path
.
join
(
selectedDirectory
,
'
$fileName
(
$i
).
$ext
'
);
i
++;
}
if
(
filePath
.
startsWith
(
'http'
))
{
if
(
filePath
.
startsWith
(
'http'
))
{
final
resp
=
await
Dio
().
download
(
filePath
,
targetPath
);
final
resp
=
await
Dio
().
download
(
filePath
,
targetPath
);
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment