java队列 语音流传输_利用现有的队列进行语音朗读

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Speech.Synthesis;usingSystem.Threading;namespaceUtility

{public sealed classSound

{private static object obj = new object();private static QueuespeckList;public static void AddToPlay(stringcontent)

{if (string.IsNullOrWhiteSpace(content))return;lock(obj)

{if (speckList == null)

{

speckList= new Queue();

StartProcess();

}

speckList.Enqueue(content);

}

}public static voidStartProcess()

{

Thread speck= new Thread(() =>{while (true)

{lock(obj)

{if (speckList != null && speckList.Count > 0)

{

sendSound(speckList.Dequeue());if (speckList.Count == 0)

{

speckList= null;break;

}

}

}

}

});

speck.IsBackground= true;

speck.Start();

}//保存上次播放的语言

private static string lastString = string.Empty;//保存默认的信息

private static string firstString = string.Empty;public static void SpeckNoRepeate(stringsound)

{if (string.IsNullOrWhiteSpace(firstString))//第一次赋值

{if(sound.Equals(ConfigHelper.GateInfo.InitialMessage))

{

firstString=sound;

}

}else{if(sound.Equals(firstString))

{return;

}

sound+= " " +sound;

}//如果和上一次的信息一致,则取消发送语音,可以避免多次重复刷卡

if(lastString.Equals(sound))

{return;

}

sendSound(sound);

lastString=sound;

}private static void sendSound(stringsound)

{using (SpeechSynthesizer _speaker = newSpeechSynthesizer())

{

_speaker.SelectVoiceByHints(VoiceGender.Female);

PromptBuilder prompt= newPromptBuilder();

PromptStyle style= newPromptStyle

{

Rate=PromptRate.Slow,

Volume=PromptVolume.ExtraLoud

};

prompt.StartStyle(style);

prompt.AppendText(Changestring(sound));

prompt.EndStyle();

_speaker.Speak(prompt);

}

}private static string Changestring(stringinput)

{return input.Replace(" ", "、")

.Replace("0", "零")

.Replace("1", "一")

.Replace("2", "二")

.Replace("3", "三")

.Replace("4", "四")

.Replace("5", "五")

.Replace("6", "六")

.Replace("7", "七")

.Replace("8", "八")

.Replace("9", "九")

.Replace("-", "杠")

.Replace("A", "A")

.Replace("B", "B")

.Replace("C", "C")

.Replace("D", "D")

.Replace("E", "E")

.Replace("F", "F")

.Replace("G", "G")

.Replace("H", "H")

.Replace("I", "I")

.Replace("J", "J")

.Replace("K", "K")

.Replace("L", "L")

.Replace("M", "M")

.Replace("N", "N")

.Replace("O", "O")

.Replace("P", "P")

.Replace("Q", "Q")

.Replace("R", "R")

.Replace("S", "S")

.Replace("T", "T")

.Replace("U", "U")

.Replace("V", "V")

.Replace("W", "W")

.Replace("X", "X")

.Replace("Y", "Y")

.Replace("Z", "Z")

.Replace('#', ' ')

;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Java 多线程队列实例,模拟队列实现排队叫号的过程: ```java import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class QueueExample { private Queue<Integer> queue = new LinkedList<>(); private Lock lock = new ReentrantLock(); private int currentTicketNumber = 0; public void addCustomer() { lock.lock(); try { queue.offer(currentTicketNumber++); System.out.println("新顾客取得号码:" + (currentTicketNumber - 1) + ",当前队列长度:" + queue.size()); } finally { lock.unlock(); } } public void serveCustomer() { lock.lock(); try { Integer ticketNumber = queue.poll(); if (ticketNumber == null) { System.out.println("队列为空,无法叫号"); } else { System.out.println("叫到号码:" + ticketNumber + ",当前队列长度:" + queue.size()); } } finally { lock.unlock(); } } public static void main(String[] args) { final QueueExample queueExample = new QueueExample(); // 开启 5 个线程模拟 5 个顾客取号 for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { queueExample.addCustomer(); } }).start(); } // 开启 3 个线程模拟 3 个服务员叫号 for (int i = 0; i < 3; i++) { new Thread(new Runnable() { @Override public void run() { queueExample.serveCustomer(); } }).start(); } } } ``` 这个例子中,我们定义了一个 `QueueExample` 类,其中有一个 `queue` 队列,用于存储所有顾客的票号。我们使用 `ReentrantLock` 类来实现线程安全。在 `addCustomer` 方法中,我们通过 `offer` 方法往队列中添加一个顾客的票号,并输出当前队列长度以及新顾客的票号。在 `serveCustomer` 方法中,我们通过 `poll` 方法从队列中获取一个顾客的票号,并输出当前队列长度以及叫到的顾客的票号。 在 `main` 方法中,我们开启了 5 个线程模拟 5 个顾客取号,以及 3 个线程模拟 3 个服务员叫号。你可以运行这个例子来看一下具体的输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值