//语音对讲
public bool SpeackVoice(int IdentifyID,int UlChannelId, string filePath)
{
bool status = false;
try
{
LPPU_VOICE_INFO_S lppu = new LPPU_VOICE_INFO_S();
lppu.ulChannelId = UlChannelId;
lppu.szLocalAudioIp = "0.0.0.0";
lppu.bFeedAudioData = true;
lppu.szReserved = string.Empty;
int ulVoiceTBHandle = SDK.IVS_PU_StartVoiceBroadcast(IdentifyID, ref lppu);
int k = SDK.IVS_PU_GetLastError();
var erro = SDK.IVS_PU_GetErrorMsg(k);
Log.Info($"华为sdk-IVS_PU_StartVoiceBroadcast方法返回信息:{erro}");
int readLength = 0; //定义读取的长度
long sentFileLength = 0;//定义发送的长度
int iOneLength = 340 * 3;
byte[] buffer = new byte[iOneLength];
using (FileStream fsRead = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
{
long fileLength = fsRead.Length;//文件长度
while ((readLength = fsRead.Read(buffer, 0, buffer.Length)) > 0 && sentFileLength < fileLength)
{
sentFileLength += readLength;
IntPtr pName = IntPtr.Zero;
pName = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, pName, buffer.Length);
SDK.IVS_PU_FeedVoiceData(IdentifyID, ulVoiceTBHandle, pName, readLength, PU_ENCODE_TYPE_E.PU_ENC_G711U);
Marshal.FreeHGlobal(pName);//释放
Thread.Sleep(129);
}
Thread.Sleep(1000);
fsRead.Close();
}
bool stopstatus = SDK.IVS_PU_StopVoiceBroadcast(IdentifyID, ulVoiceTBHandle);//关闭语音播报
int r = SDK.IVS_PU_GetLastError();
var erro3 = SDK.IVS_PU_GetErrorMsg(r);
DelectDir(filePath);//播报完毕删除文件
if (stopstatus)
{
Log.Info($"关闭语音播报成功--{DateTime.Now}");
}
else
{
Log.Info($"关闭语音播报失败{erro3}--{DateTime.Now}");
}
bool Cancelstatus = SDK.IVS_PU_Logout(IdentifyID);//注销设备
if (Cancelstatus)
{
Log.Info($"注销设备成功--{DateTime.Now}");
}
else
{
Log.Info($"注销设备失败--{DateTime.Now}");
}
}
catch (Exception ex)
{
Log.Error($"语音播报错误{ex.Message}");
}
finally
{
var a = IPCLogout(IdentifyID);
}
return status;
}
来源:https://www.cnblogs.com/macT/p/11821246.html