完善对讲语音发送功能

This commit is contained in:
魏少阳 2023-12-21 16:48:07 +08:00
parent 5a5f8ea42f
commit 535d274c6e
5 changed files with 135 additions and 38 deletions

View File

@ -39,6 +39,10 @@
<!--允许读设备等信息,用于问题排查-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--允许麦克风权限,用于录音发送-->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:label="星锁"
android:name="${applicationName}"

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../../../tools/baseGetXController.dart';
import '../../../../tools/eventBusEventManage.dart';
@ -33,6 +34,30 @@ class LockMonitoringLogic extends BaseGetXController {
});
}
Future<bool> getPermissionStatus() async {
Permission permission = Permission.microphone;
//granted denied permanentlyDenied
PermissionStatus status = await permission.status;
if (status.isGranted) {
return true;
} else if (status.isDenied) {
requestPermission(permission);
} else if (status.isPermanentlyDenied) {
openAppSettings();
} else if (status.isRestricted) {
requestPermission(permission);
} else {}
return false;
}
///
void requestPermission(Permission permission) async {
PermissionStatus status = await permission.request();
if (status.isPermanentlyDenied) {
openAppSettings();
}
}
@override
void onReady() {
// TODO: implement onReady

View File

@ -1,9 +1,12 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:get/get.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import '../../../../app_settings/app_colors.dart';
import '../../../../talk/udp/udp_manage.dart';
@ -160,22 +163,48 @@ class _LockMonitoringPageState extends State<LockMonitoringPage> {
Widget bottomBottomBtnWidget() {
return Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
//
Obx(() => bottomBtnItemWidget(
getAnswerBtnImg(), getAnswerBtnName(), Colors.white, () async {
var userMobileIP = await NetworkInfo().getWifiIP();
var userMobile = await Storage.getMobile();
Obx(() => bottomBtnItemWidget(getAnswerBtnImg(), getAnswerBtnName(), Colors.white, () async {
//
await logic.getPermissionStatus().then((value) async {
if (!value) {
return;
}
var userMobileIP = await NetworkInfo().getWifiIP();
var userMobile = await Storage.getMobile();
//
UDPSenderManage.sendMainProtocol(
command: 150,
commandTypeIsCalling: 1,
subCommand: 6,
lockID: UDPManage().lockId,
lockIP: UDPManage().host,
userMobile: userMobile,
userMobileIP: userMobileIP,
endData: []);
})),
//
UDPSenderManage.sendMainProtocol(
command: 150,
commandTypeIsCalling: 1,
subCommand: 6,
lockID: UDPManage().lockId,
lockIP: UDPManage().host,
userMobile: userMobile,
userMobileIP: userMobileIP,
endData: []
);
});
},
longPress: (){
//
print("onLongPress");
if (state.udpStatus.value == 8) {
state.udpStatus.value = 9;
}
_startRecording();
},
longPressUp: () async {
//
print("onLongPressUp");
if (state.udpStatus.value == 9) {
state.udpStatus.value = 8;
}
_stopRecording();
_playRecording();
}
)
),
bottomBtnItemWidget(
"images/main/icon_lockDetail_hangUp.png", "挂断", Colors.red, () async {
var userMobileIP = await NetworkInfo().getWifiIP();
@ -234,22 +263,12 @@ class _LockMonitoringPageState extends State<LockMonitoringPage> {
}
Widget bottomBtnItemWidget(
String iconUrl, String name, Color backgroundColor, Function() onClick) {
String iconUrl, String name, Color backgroundColor, Function() onClick, {Function()? longPress, Function()? longPressUp}) {
var wh = 80.w;
return GestureDetector(
onTap: onClick,
onLongPress: () {
print("onLongPress");
if (state.udpStatus.value == 8) {
state.udpStatus.value = 9;
}
},
onLongPressUp: () {
print("onLongPressUp");
if (state.udpStatus.value == 9) {
state.udpStatus.value = 8;
}
},
onLongPress: longPress,
onLongPressUp: longPressUp,
child: SizedBox(
height: 140.h,
child: Column(
@ -331,29 +350,77 @@ class _LockMonitoringPageState extends State<LockMonitoringPage> {
//
_startRecording() async {
filePath = 'your_output_file.wav';
getFilePath().then((value) {
filePath = value;
});
await recorder.startRecorder(
toFile: filePath,
codec: Codec.pcm16WAV,
);
}
//
//
_stopRecording() async {
await recorder.stopRecorder();
var userMobileIP = await NetworkInfo().getWifiIP();
var userMobile = await Storage.getMobile();
// final file = File(filePath);
File file = File(filePath); // 使 create
print('filePathfilePath:$filePath file:$file await file.exists():${await file.exists()}');
if (await file.exists()) {
final List<int> bytes = await file.readAsBytes();
print('Recorded audio bytes.length:${bytes.length} bytes: $bytes');
//
UDPSenderManage.sendMainProtocol(
command: 150,
commandTypeIsCalling: 1,
subCommand: 8,
lockID: UDPManage().lockId,
lockIP: UDPManage().host,
userMobile: userMobile,
userMobileIP: userMobileIP,
endData: bytes
);
}
}
//
Future<String> getFilePath() async {
final directory = await getApplicationDocumentsDirectory();
final filePath = '${directory.path}/recording.wav';
//
File file = File(filePath);
await file.create(); // 使 create
return filePath;
}
Future<void> _getRecordedAudioBytes() async {
final file = File(filePath);
if (await file.exists()) {
final List<int> bytes = await file.readAsBytes();
print('Recorded audio bytes: $bytes');
}
}
//
_playRecording() async {
player = FlutterSoundPlayer();
await player.startPlayer(
fromURI: filePath,
codec: Codec.pcm16WAV,
);
Toast.show(msg: "储存录音播放了");
print('_playRecording() 储存录音播放了');
}
//
_stopPlaying() async {
await player.stopPlayer();
}
// _stopPlaying() async {
// await player.stopPlayer();
// }
}

View File

@ -43,11 +43,12 @@ class CallTalk {
Future<void> getAVData(Uint8List bb, int len) async {
//
if (bb[61] == 1) {
print('dinglingling音频数据来啦啦啦啦啦啦啦啦啦$bb');
print('dinglingling bb.length:${bb.length} 音频数据来:$bb ');
// 711Uint8List
// Uint8List rawData = G711Decoder().decodeG711uLaw(bb);
// G711 u-law a-law
Uint8List g711Data = bb;
Uint8List g711Data = bb.sublist(77, bb.length);
// PCM
Uint8List pcmData = G711Decoder().g711Decode(g711Data, G711Type.uLaw);
// 使 pcmData

View File

@ -19,7 +19,7 @@ class CommandUDPReciverManager {
if (dataSize < 4) {
return;
}
print("appReceiveUDPData:$data");
// print("appReceiveUDPData:$data");
Uint8List data1 = Uint8List.fromList(data);
if (data1.length == 1) {
@ -43,7 +43,7 @@ class CommandUDPReciverManager {
//
var beiCallType = data[8] & 0xff;
print("被呼叫类型$beiCallType");
// print("被呼叫类型$beiCallType");
switch (beiCallType) {
case 1:
{