diff --git a/DevicesService/Commen/COMUtils.cs b/DevicesService/Commen/COMUtils.cs
index 6b6bafe..f9771c7 100644
--- a/DevicesService/Commen/COMUtils.cs
+++ b/DevicesService/Commen/COMUtils.cs
@@ -28,6 +28,8 @@ namespace DevicesService.Common
{
try
{
+
+
string COMName = ConfigurationManager.AppSettings["COMName"].ToString();
// 设置COM口,波特率,奇偶校验,数据位,停止位
serialPort.PortName = COMName; // 请替换为你的串口名称
@@ -55,7 +57,6 @@ namespace DevicesService.Common
{
Log.Error("服务启动异常: " + ex.Message + "");
}
-
}
///
@@ -234,6 +235,21 @@ namespace DevicesService.Common
SendData(base64);
#endregion
break;
+ //播放音频:{"type":"12","param":{"ispaly":true,"url":"http://192.168.0.34:92/test.wav"}}
+ //恢复播放:{ "type":"12","param":{ "ispaly":true,"url":""} }
+ //暂停播放:{"type":"12","param":{"ispaly":false,"url":""}}
+ case Func.Paly:
+ #region 播放与暂停
+ joparam = (JObject)JsonConvert.DeserializeObject(param);
+ bool ispaly = Convert.ToBoolean(joparam["ispaly"].ToString());
+ string purl = joparam["url"].ToString();
+ resultback = scriptCallback.Paly(purl, ispaly, callback);
+ Log.Info("播放与暂停 返回数据:" + resultback);
+ base64 = Util.str2Base64(resultback);
+ //向com对方发送数据
+ SendData(base64);
+ #endregion
+ break;
default:
string str = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "调用方法不存在" + "\"}";
base64 = Util.str2Base64(str);
diff --git a/DevicesService/Commen/Func.cs b/DevicesService/Commen/Func.cs
index b431d16..4f9924d 100644
--- a/DevicesService/Commen/Func.cs
+++ b/DevicesService/Commen/Func.cs
@@ -52,5 +52,9 @@ namespace DevicesService.Commen
/// 开始录音、取消录音、结束录音
///
public const int SoundRecording = 11;
+ ///
+ /// 音频文件播放与暂停
+ ///
+ public const int Paly = 12;
}
}
diff --git a/DevicesService/Commen/ScriptCallbackObject.cs b/DevicesService/Commen/ScriptCallbackObject.cs
index 0973235..a12fdcc 100644
--- a/DevicesService/Commen/ScriptCallbackObject.cs
+++ b/DevicesService/Commen/ScriptCallbackObject.cs
@@ -23,6 +23,10 @@ using System.Threading.Tasks;
using System.Threading;
using static System.Net.Mime.MediaTypeNames;
using System.Text.RegularExpressions;
+using DevicesService.Commen;
+using System.Numerics;
+using System.Web;
+using NAudio.Wave.SampleProviders;
namespace DevicesService.Common
{
@@ -35,7 +39,7 @@ namespace DevicesService.Common
public HScamera hScamera = new HScamera(0, 0, 1);
private static SpeechSynthesizer speech = new SpeechSynthesizer();
//读取身份证
- public string IDCardRead(string paramsString,string callback)
+ public string IDCardRead(string paramsString, string callback)
{
Task.Run(() =>
GetCardInfo()
@@ -96,7 +100,7 @@ namespace DevicesService.Common
///
///
///
- public string payleText(string text, bool ispaye,string callback)
+ public string payleText(string text, bool ispaye, string callback)
{
try
{
@@ -190,7 +194,7 @@ namespace DevicesService.Common
/// 打开高拍仪并且进行快速扫描文件
///
///
- public string openCamera(string url,string callback)
+ public string openCamera(string url, string callback)
{
try
{
@@ -199,7 +203,7 @@ namespace DevicesService.Common
{
//string base64 = Util.ImgToBase64String(data);
-
+
Task.Run(async () =>
{
UploadInfoByFile(url, data);
@@ -243,7 +247,7 @@ namespace DevicesService.Common
/// 打开签字版数据
///
///
- public string OpenSign(string paramsString,string callback)
+ public string OpenSign(string paramsString, string callback)
{
try
{
@@ -285,7 +289,7 @@ namespace DevicesService.Common
/// 关闭签字版数据
///
///
- public string CloseSign(string paramsString,string callback)
+ public string CloseSign(string paramsString, string callback)
{
try
{
@@ -511,11 +515,144 @@ namespace DevicesService.Common
return "{\"callback\":\"" + callback + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}";
}
}
-
+
+ ///
+ /// 播放与暂停
+ ///
+ ///
+ public static NAudio.Wave.WaveOutEvent player = null;
+ public static AudioFileReader audioFileReader = null;
+ public static string WaveOutPath = string.Empty;
+ public string Paly(string url, bool ispaly, string callback)
+ {
+ try
+ {
+ if (ispaly)
+ {
+ if (player == null)
+ {
+ NewMethod(url);
+ }
+ else
+ {
+ if (string.IsNullOrEmpty(url))
+ {
+ player.Play();
+ Log.Info("恢复播放");
+ }
+ else
+ {
+ if (player.PlaybackState == PlaybackState.Playing)
+ {
+ player.Stop();
+ // 释放资源
+ player.Dispose();
+ audioFileReader.Dispose();
+ }
+ NewMethod(url);
+ }
+ }
+ }
+ else
+ {
+ if (player != null)
+ {
+ player.Pause();
+ Log.Info("暂停播放");
+ }
+ }
+ if (player != null)
+ {
+ return "{\"callback\":\"" + callback + "\",\"message\":\"success\",\"code\":\"200\",\"status\":true,\"data\":\"" + "音频播放" + "\"}";
+ }
+ else
+ {
+ return "{\"callback\":\"" + callback + "\",\"message\":\"success\",\"code\":\"400\",\"status\":false,\"data\":\"" + "未发现播放文件" + "\"}";
+ }
+ }
+ catch (Exception ex)
+ {
+ return "{\"callback\":\"" + callback + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}";
+ }
+ }
+
+ ///
+ /// 播放音频公共方法
+ ///
+ ///
+ private void NewMethod(string url)
+ {
+ Uri uri = new Uri(url);
+ //返回 123.xlsx
+ var filename = HttpUtility.UrlDecode(uri.Segments.Last());
+ //获取文件后缀
+ string ext = Path.GetExtension(filename);
+ DateTime dateTime = DateTime.Now;
+ string time = DateTime.Now.ToString(
+ "yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);
+ var dirpath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "WaveOutFile");
+ FileExt.MakeSureDirExist(dirpath);
+ var filepath = Path.Combine(dirpath, time);
+ WaveOutPath = dirpath + "\\" + time + ext;
+ WebRequest request = WebRequest.Create(url);
+ WebResponse response = request.GetResponse();
+ using (Stream stream = response.GetResponseStream())
+ {
+ using (FileStream fileStream = new FileStream(WaveOutPath, FileMode.Create))
+ {
+ stream.CopyTo(fileStream);
+ }
+ }
+ response.Close();
+ using (player = new WaveOutEvent())
+ {
+ player.PlaybackStopped += waveOut_PlaybackStopped;
+ using (audioFileReader = new AudioFileReader(WaveOutPath))
+ {
+ // 创建一个增益效果器对象
+ var volumeProvider = new VolumeSampleProvider(audioFileReader.ToSampleProvider());
+ // 将音频文件添加到WaveOutEvent对象中
+ player.Init(volumeProvider);
+ // 设置音量增益为2倍
+ volumeProvider.Volume = 2.0f;
+ // 开始播放音频
+ player.Play();
+ while (player.PlaybackState == PlaybackState.Playing)
+ {
+ Thread.Sleep(1000);
+ }
+ }
+ }
+ Log.Info("开始播放");
+ }
+
+ ///
+ /// 播放完成
+ ///
+ ///
+ ///
+ private void waveOut_PlaybackStopped(object sender, StoppedEventArgs e)
+ {
+ try
+ {
+ player.Stop();
+ // 释放资源
+ player.Dispose();
+ audioFileReader.Dispose();
+ player = null;
+ File.Delete(WaveOutPath);
+ Log.Info("播放完成");
+ }
+ catch (Exception ex)
+ {
+ Log.Info("播放完成,清除本地文件异常" + ex.Message);
+ }
+ }
+
//上传本地文件中转
private string urlP = string.Empty;
private AutoResetEvent @event5 = new AutoResetEvent(false);
- private async void UploadInfoByFile(string url,string strpath)
+ private async void UploadInfoByFile(string url, string strpath)
{
var httpClient = new HttpClient();
var uploader = new ChunkedUpload(httpClient);
@@ -523,7 +660,7 @@ namespace DevicesService.Common
File.Delete(strpath);
@event5.Set();
}
-
+
//上传录音
private string urlpath = string.Empty;
private AutoResetEvent @event2 = new AutoResetEvent(false);
@@ -532,7 +669,7 @@ namespace DevicesService.Common
var httpClient = new HttpClient();
var uploader = new ChunkedUpload(httpClient);
urlpath = await uploader.UploadFileAsync(url, srpath);
- File.Delete(srpath);
+ //File.Delete(srpath);
srpath = string.Empty;
@event2.Set();
}
diff --git a/DevicesService/Devices/HScamera.cs b/DevicesService/Devices/HScamera.cs
index 7c35848..de83ebf 100644
--- a/DevicesService/Devices/HScamera.cs
+++ b/DevicesService/Devices/HScamera.cs
@@ -143,9 +143,9 @@ namespace DevicesService.Devices
///
public string ImgToBase64String(string fileLocation)
{
- MemoryStream ms = new MemoryStream();
try
{
+ MemoryStream ms = new MemoryStream();
if (System.IO.File.Exists(fileLocation))
{
Bitmap bmp = new Bitmap(fileLocation);
@@ -163,8 +163,8 @@ namespace DevicesService.Devices
var filepath = Path.Combine(dirpath, time);
string filename = dirpath + "\\" + time + ".jpg";
ConvertFromBase64ToImage(base64, filename);
- ms.Close();
- File.Delete(fileLocation);
+ //ms.Close();
+ //File.Delete(fileLocation);
return filename;
}
return "";
diff --git a/DevicesService/Program.cs b/DevicesService/Program.cs
index 099eb26..cbe2190 100644
--- a/DevicesService/Program.cs
+++ b/DevicesService/Program.cs
@@ -2,6 +2,7 @@
using DevicesService.Common;
using System;
using System.Diagnostics;
+using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
@@ -22,5 +23,18 @@ namespace DevicesService
Task.Delay(2000).Wait();
}
}
+ static bool IsProgramRunning(string programName)
+ {
+ Process[] processes = Process.GetProcesses();
+ foreach (Process process in processes)
+ {
+ if (process.ProcessName.Equals(programName))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
}
}