This commit is contained in:
zpc 2025-03-28 05:11:53 +08:00
parent 9bf8e00754
commit 32a157d083
5 changed files with 57 additions and 22 deletions

View File

@ -254,5 +254,10 @@ public class CallFlowConfig
/// <summary>
/// 播放电话接起音频的概率0-1之间的小数
/// </summary>
public double PlayPickupProbability { get; set; } = 0.15;
public float PlayPickupProbability { get; set; } = 0.15f;
/// <summary>
/// 用户挂断后是否重置系统false表示直接退出程序
/// </summary>
public bool ResetSystemAfterHangup { get; set; } = true;
}

View File

@ -99,11 +99,11 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
_audioFileService = audioFileService;
// 获取初始配置
_config = _signalRService.GetConfigAsync().GetAwaiter().GetResult();
_config = _signalRService.GetConfigAsync().GetAwaiter().GetResult();
// 注册事件处理
_signalRService.ConfigUpdated += SignalRService_ConfigUpdated;
_signalRService.AudioFileUpdated += SignalRService_AudioFileUpdated;
//_signalRService.ConfigUpdated += SignalRService_ConfigUpdated;
//_signalRService.AudioFileUpdated += SignalRService_AudioFileUpdated;
}
/// <summary>
@ -608,7 +608,7 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
// 确保背景音乐停止
StopBackgroundMusic();
Console.WriteLine("录音结束,重置状态...");
Console.WriteLine("录音结束,处理中...");
// 尝试上传录音文件到服务器
if (File.Exists(_recordingFilePath))
@ -635,10 +635,27 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
});
}
// 完全重置系统状态
CompletelyResetState();
Console.WriteLine("系统已重置,可以继续使用...");
// 检查是否由用户挂断或静音触发的结束
bool isUserHangup = _isHangUpKeyPressed || (DateTime.Now - _lastSoundTime).TotalSeconds >= _config.Recording.SilenceTimeoutSeconds;
// 根据配置决定是重置系统还是退出程序
if (isUserHangup && !_config.CallFlow.ResetSystemAfterHangup)
{
Console.WriteLine("用户挂断或静音超时,根据配置将退出程序...");
// 完全重置系统状态
//CompletelyResetState();
// 确保所有资源被释放
StopAsync().Wait();
// 退出程序
Environment.Exit(0);
}
else
{
// 完全重置系统状态
CompletelyResetState();
Console.WriteLine("系统已重置,可以继续使用...");
}
}
catch (OperationCanceledException ex)
{
@ -647,10 +664,23 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
// 确保录音设备被释放
StopAudioRecording();
// 取消时也完全重置系统状态
CompletelyResetState();
Console.WriteLine("系统已重置,可以继续使用...");
// 检查是否需要退出程序
if (_isHangUpKeyPressed && !_config.CallFlow.ResetSystemAfterHangup)
{
Console.WriteLine("用户挂断,根据配置将退出程序...");
// 确保所有资源被释放
StopAsync().Wait();
// 退出程序
Environment.Exit(0);
}
else
{
// 取消时也完全重置系统状态
CompletelyResetState();
Console.WriteLine("系统已重置,可以继续使用...");
}
}
catch (Exception ex)
{
@ -686,6 +716,8 @@ public class PhoneBoothService : IPhoneBoothService, IDisposable
if ((DateTime.Now - _lastSoundTime).TotalSeconds >= silenceTimeout)
{
Console.WriteLine($"检测到{silenceTimeout}秒无声音,自动挂断");
// 将静音超时也视为用户挂断的一种形式
_isHangUpKeyPressed = true;
completionSource?.TrySetResult(true);
}
}

View File

@ -346,13 +346,13 @@ public class SignalRService : ISignalRService
await _semaphore.WaitAsync();
try
{
_currentConfig = newConfig;
//_currentConfig = newConfig;
// 保存到本地备份
_currentConfig.SaveToFile(_configBackupPath);
//// 保存到本地备份
//_currentConfig.SaveToFile(_configBackupPath);
// 触发配置更新事件
ConfigUpdated?.Invoke(this, _currentConfig);
//// 触发配置更新事件
//ConfigUpdated?.Invoke(this, _currentConfig);
}
finally
{

View File

@ -31,6 +31,7 @@
"CallFlow": {
"WaitForPickupMinSeconds": 3,
"WaitForPickupMaxSeconds": 6,
"PlayPickupProbability": 0.5
"PlayPickupProbability": 0.5,
"ResetSystemAfterHangup": false
}
}

View File

@ -413,9 +413,6 @@ namespace ShengShengBuXi.Hubs
_logger.LogInformation($"结束音频流: {Context.ConnectionId}");
// 结束音频处理
await _audioProcessingService.EndAudioStreamAsync(Context.ConnectionId);
// 结束语音识别会话