|
|
@ -23,6 +23,10 @@ using System.Threading.Tasks; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading; |
|
|
|
using static System.Net.Mime.MediaTypeNames; |
|
|
|
using static System.Net.Mime.MediaTypeNames; |
|
|
|
using System.Text.RegularExpressions; |
|
|
|
using System.Text.RegularExpressions; |
|
|
|
|
|
|
|
using DevicesService.Commen; |
|
|
|
|
|
|
|
using System.Numerics; |
|
|
|
|
|
|
|
using System.Web; |
|
|
|
|
|
|
|
using NAudio.Wave.SampleProviders; |
|
|
|
|
|
|
|
|
|
|
|
namespace DevicesService.Common |
|
|
|
namespace DevicesService.Common |
|
|
|
{ |
|
|
|
{ |
|
|
@ -512,6 +516,139 @@ namespace DevicesService.Common |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 播放与暂停 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <returns></returns> |
|
|
|
|
|
|
|
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 + "\"}"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 播放音频公共方法 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="url"></param> |
|
|
|
|
|
|
|
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("开始播放"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// 播放完成 |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="sender"></param> |
|
|
|
|
|
|
|
/// <param name="e"></param> |
|
|
|
|
|
|
|
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 string urlP = string.Empty; |
|
|
|
private AutoResetEvent @event5 = new AutoResetEvent(false); |
|
|
|
private AutoResetEvent @event5 = new AutoResetEvent(false); |
|
|
@ -532,7 +669,7 @@ namespace DevicesService.Common |
|
|
|
var httpClient = new HttpClient(); |
|
|
|
var httpClient = new HttpClient(); |
|
|
|
var uploader = new ChunkedUpload(httpClient); |
|
|
|
var uploader = new ChunkedUpload(httpClient); |
|
|
|
urlpath = await uploader.UploadFileAsync(url, srpath); |
|
|
|
urlpath = await uploader.UploadFileAsync(url, srpath); |
|
|
|
File.Delete(srpath); |
|
|
|
//File.Delete(srpath); |
|
|
|
srpath = string.Empty; |
|
|
|
srpath = string.Empty; |
|
|
|
@event2.Set(); |
|
|
|
@event2.Set(); |
|
|
|
} |
|
|
|
} |
|
|
|