Commit 78bc113a by tanghuan

音量检测,以及确保在iOS中不使用听筒模式

1 parent ad30fbd2
......@@ -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();
......
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;
}
}
......
......@@ -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);
}
......
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())!;
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!