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 78bc113a
authored
2025-12-23 15:34:55 +0800
by
tanghuan
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
音量检测,以及确保在iOS中不使用听筒模式
1 parent
ad30fbd2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
18 deletions
lib/bloc/web_cubit.dart
lib/data/repositories/message/audio_player_handler.dart
lib/services/dispatcher.dart
lib/services/player_service.dart
lib/bloc/web_cubit.dart
View file @
78bc113
...
...
@@ -22,6 +22,7 @@ import 'package:path_provider/path_provider.dart';
import
'package:permission_handler/permission_handler.dart'
;
import
'package:shared_preferences/shared_preferences.dart'
;
import
'package:uuid/uuid.dart'
;
import
'package:volume_controller/volume_controller.dart'
;
import
'package:webview_flutter/webview_flutter.dart'
;
import
'package:wechat_assets_picker/wechat_assets_picker.dart'
;
import
'package:wechat_camera_picker/wechat_camera_picker.dart'
;
...
...
@@ -1121,6 +1122,27 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
_sendResponse
(
resp
);
}
bool
_isFirstTimeOfCheck
=
true
;
/// 检测音量,太小时,发出提示
///
Future
<
void
>
checkVolume
()
async
{
if
(
_isFirstTimeOfCheck
)
{
VolumeController
volumeController
=
VolumeController
.
instance
;
var
vol
=
await
volumeController
.
getVolume
();
debugPrint
(
'检测音量:
$vol
'
);
if
(
vol
==
0
)
{
emit
(
state
.
copyWith
(
showSnackBar:
true
,
snackBarMsg:
'设备处于静音状态,请调整音量'
));
emit
(
state
.
copyWith
(
showSnackBar:
false
,
snackBarMsg:
''
));
_isFirstTimeOfCheck
=
false
;
}
else
if
(
vol
<=
0.15
)
{
emit
(
state
.
copyWith
(
showSnackBar:
true
,
snackBarMsg:
'设备音量过低,建议调高音量'
));
emit
(
state
.
copyWith
(
showSnackBar:
false
,
snackBarMsg:
''
));
_isFirstTimeOfCheck
=
false
;
}
}
}
Future
<
void
>
_checkLocalServerStatus
()
async
{
var
localServerService
=
getIt
.
get
<
LocalServerService
>();
var
result
=
await
localServerService
.
checkServerHealth
();
...
...
lib/data/repositories/message/audio_player_handler.dart
View file @
78bc113
import
'package:appframe/bloc/web_cubit.dart'
;
import
'package:appframe/config/locator.dart'
;
import
'package:appframe/services/dispatcher.dart'
;
import
'package:appframe/services/player_service.dart'
;
import
'package:uuid/uuid.dart'
;
class
AudioPlayHandler
extends
MessageHandler
{
late
WebCubit
?
_webCubit
;
@override
Future
<
dynamic
>
handleMessage
(
dynamic
params
)
async
{
if
(
params
is
!
Map
<
String
,
dynamic
>)
{
throw
Exception
(
'参数错误'
);
}
try
{
if
(
params
is
!
Map
<
String
,
dynamic
>)
{
throw
Exception
(
'参数错误'
);
}
final
url
=
params
[
'url'
]
as
String
;
final
seek
=
params
[
'seek'
]
as
int
?
??
0
;
// 暂时忽略
// final isBg = params['isBg'] as bool ?? false;
final
playRate
=
(
params
[
'playRate'
]
as
num
?)?.
toDouble
()
??
1.0
;
// 检测音量
_webCubit
!.
checkVolume
();
var
playId
=
params
[
'playId'
]
as
String
?
??
''
;
if
(
playId
.
isEmpty
)
{
playId
=
Uuid
().
v4
();
}
final
url
=
params
[
'url'
]
as
String
;
final
seek
=
params
[
'seek'
]
as
int
?
??
0
;
// 暂时忽略
// final isBg = params['isBg'] as bool ?? false;
final
playRate
=
(
params
[
'playRate'
]
as
num
?)?.
toDouble
()
??
1.0
;
var
playerService
=
getIt
.
get
<
PlayerService
>();
var
result
=
await
playerService
.
playAudio
(
url
,
seek
,
playId
,
playRate
);
var
playId
=
params
[
'playId'
]
as
String
?
??
''
;
if
(
playId
.
isEmpty
)
{
playId
=
Uuid
().
v4
();
}
if
(!
result
)
{
throw
Exception
(
'播放错误'
);
var
playerService
=
getIt
.
get
<
PlayerService
>();
var
result
=
await
playerService
.
playAudio
(
url
,
seek
,
playId
,
playRate
);
if
(!
result
)
{
throw
Exception
(
'播放错误'
);
}
return
{
'playId'
:
playId
};
}
finally
{
_unfollowCubit
();
}
return
{
'playId'
:
playId
};
}
@override
void
setCubit
(
WebCubit
cubit
)
{
this
.
_webCubit
=
cubit
;
}
void
_unfollowCubit
()
{
this
.
_webCubit
=
null
;
}
}
...
...
lib/services/dispatcher.dart
View file @
78bc113
...
...
@@ -54,7 +54,8 @@ class MessageDispatcher {
h5Message
.
cmd
==
"chooseImage"
||
h5Message
.
cmd
==
"chooseVideo"
||
h5Message
.
cmd
==
"goLogin"
||
h5Message
.
cmd
.
startsWith
(
"setTitlebar"
))
{
h5Message
.
cmd
.
startsWith
(
"setTitlebar"
)
||
h5Message
.
cmd
==
"audioPlay"
)
{
handler
.
setCubit
(
webCubit
!);
handler
.
setMessage
(
message
);
}
...
...
lib/services/player_service.dart
View file @
78bc113
import
'dart:async'
;
import
'dart:io'
;
import
'package:audio_session/audio_session.dart'
;
import
'package:flutter_sound/flutter_sound.dart'
;
class
PlayerService
{
...
...
@@ -27,6 +29,12 @@ class PlayerService {
Future
<
bool
>
_initPlayer
()
async
{
// 打开播放器
try
{
// 针对iOS, 需要控制不使用听筒进行播放
if
(
Platform
.
isIOS
)
{
var
audioSession
=
await
AudioSession
.
instance
;
await
audioSession
.
configure
(
AudioSessionConfiguration
.
music
());
}
final
player
=
FlutterSoundPlayer
();
_player
=
(
await
player
.
openPlayer
())!;
...
...
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