gpt4 book ai didi

用于带有 GSM 调制解调器 rxtx 的 IVRS 的 Java 多线程(播放语音文件使事件监听器停止工作)

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:02:28 26 4
gpt4 key购买 nike

我已经实现了一个程序来使用 gsm 调制解调器接听电话。在检测到 “RING” 调用被应答时,通过从 DATA_AVAILABLE 事件处理程序内部调用函数来播放音频剪辑。但是事件处理程序在此之后停止工作。音频完成后,事件处理程序不再显示任何数据接收事件。

为什么事件监听器停止工作。从事件处理程序内部播放音频是我做错了吗?我正在考虑从 data_received 事件处理程序内部设置一个变量 true 或 false 并创建自定义事件处理程序来监听对该变量的更改以播放音频,这两者是否可以同时工作?

如何创建多线程解决方案,使串行 I/O 不被中断,并且音频播放和音频采样可以以同步方式完成以检测 dtmf 音调。有什么办法可以不间断地监听串口事件,并在特定时间运行音频采样和音频播放的功能

在 switch 的这种情况下接受调用,线程在 play() 函数中启动

 case SerialPortEvent.DATA_AVAILABLE:

StringBuffer sb = new StringBuffer();
byte[] readBuffer = new byte[2048];
try {
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
sb.append(new String(readBuffer,0,numBytes));
System.out.println(numBytes);
System.out.println(sb);
}
System.out.println("Data Available");

if((sb.toString()).contains("RING")){
System.out.println("Enter Inside if RING Loop");
//play();
send("ATA\r\n");

//welcomeMessage();
}

if((sb.toString()).contains("CARRIER")){

hangup();
//Thread.sleep(1000);
closePort();
outCommand();
System.out.println("Enter Inside if NO CARRIER Loop");
}
//print response message
System.out.print(sb.toString());
} catch (IOException e) {
}

break;

 public void play() {
try {
new Thread() {
public void run() {
for(int i=0;i<1;i++)
welcomeMessage();
}
}.start();
} catch (Throwable e) {
e.printStackTrace();
}
}

完整代码

package sample;

import java.io.*;
import java.util.*;
import javax.sound.sampled.*;
import javazoom.jl.player.*;
import java.io.FileInputStream;
import gnu.io.*;
import java.io.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.apache.log4j.chainsaw.Main;
import sun.audio.*;

public class GSMConnect implements SerialPortEventListener,
CommPortOwnershipListener {

private static String comPort = "COM3"; // This COM Port must be connect with GSM Modem or your mobile phone
private String messageString = "";
private CommPortIdentifier portId = null;
private Enumeration portList;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private SerialPort serialPort;
String readBufferTrial = "";
/** Creates a new instance of GSMConnect */
public GSMConnect(String comm) {

this.comPort = comm;

}

public boolean init() {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comPort)) {
System.out.println("Got PortName");
return true;
}
}
}
return false;
}

public void checkStatus() {
send("AT+CREG?\r\n");
}

public void dial(String phoneNumber) {
try {
//dial to this phone number

messageString = "ATD" + phoneNumber + ";\r\n";
outputStream.write(messageString.getBytes());
System.out.println("Called ");
} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String cmd) {
try {
outputStream.write(cmd.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}

public void sendMessage(String phoneNumber, String message) {
char quotes ='"';
send("AT+CMGS="+quotes + phoneNumber +quotes+ "\r\n");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// send("AT+CMGS=\""+ phoneNumber +"\"\r\n");
send(message + '\032');
System.out.println("Message Sent");
}

public void hangup() {
send("ATH\r\n");
}
public void welcomeMessage(){

// open the sound file as a Java input stream
String gongFile = "C:\\Users\\XXXX\\Desktop\\1-welcome.wav";

}*/
try{

FileInputStream fis = new FileInputStream("C:\\Users\\XXXX\\Desktop\\7001110.mp3");
Player playMP3 = new Player(fis);

playMP3.play();
System.out.print("welcomeMessage() Read");
}catch(Exception e){

System.out.println(e);

}
}

public void play() {
try {
new Thread() {
public void run() {
for(int i=0;i<1;i++)
welcomeMessage();
}
}.start();
} catch (Throwable e) {
e.printStackTrace();
}
}
public void connect() throws NullPointerException {
if (portId != null) {
try {
portId.addPortOwnershipListener(this);

serialPort = (SerialPort) portId.open("MobileGateWay", 2000);
serialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
} catch (PortInUseException | UnsupportedCommOperationException e) {
e.printStackTrace();
}

try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();

} catch (IOException e) {
e.printStackTrace();
}

try {
/** These are the events we want to know about*/
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.notifyOnRingIndicator(true);

} catch (TooManyListenersException e) {
e.printStackTrace();
}

//Register to home network of sim card

send("ATZ\r\n");

} else {
throw new NullPointerException("COM Port not found!!");
}
}

public void serialEvent(SerialPortEvent serialPortEvent) {
System.out.println("serialPortEvent.getEventType()"+serialPortEvent.getEventType());
switch (serialPortEvent.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
// System.out.println("Ringing");
if( serialPortEvent.getNewValue() )
{
System.out.println("Ring Indicator On");
}
else
{
System.out.println("Ring Indicator Off");
}

break;



case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
case SerialPortEvent.DATA_AVAILABLE:

StringBuffer sb = new StringBuffer();
byte[] readBuffer = new byte[2048];
try {
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
sb.append(new String(readBuffer,0,numBytes));
System.out.println(numBytes);
System.out.println(sb);
}
System.out.println("Data Available");

if((sb.toString()).contains("RING")){
System.out.println("Enter Inside if RING Loop");
//play();
send("ATA\r\n");

//welcomeMessage();
}

if((sb.toString()).contains("CARRIER")){

hangup();
//Thread.sleep(1000);
closePort();
outCommand();
System.out.println("Enter Inside if NO CARRIER Loop");
}
//print response message
System.out.print(sb.toString());
} catch (IOException e) {
}

break;
}
}

public void outCommand(){
System.out.print(readBufferTrial);
}
public void ownershipChange(int type) {
switch (type) {
case CommPortOwnershipListener.PORT_UNOWNED:
System.out.println(portId.getName() + ": PORT_UNOWNED");
break;
case CommPortOwnershipListener.PORT_OWNED:
System.out.println(portId.getName() + ": PORT_OWNED");
break;
case CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED:
System.out.println(portId.getName() + ": PORT_INUSED");
break;
}
}
public void closePort(){

serialPort.close();
}

public static void main(String args[]) {
GSMConnect gsm = new GSMConnect(comPort);
if (gsm.init()) {
try {
System.out.println("Initialization Success");
gsm.connect();
Thread.sleep(5000);
gsm.checkStatus();
Thread.sleep(5000);
// System.out.println("Before Auto Answer");
// gsm.send("ATS0=5");
// gsm.dial("87XXXXXSS");
// Thread.sleep(7500);
// System.out.println("After Auto Answer set");

// gsm.sendMessage("8XXXXXS56", "Trial Success Call me");
// gsm.sendMessage("80XXXXS56", "Trial Success Call me");
// gsm.sendMessage("8XXXXSXS6", "Trial Success Call me");
// Thread.sleep(5000);
// gsm.sendMessage("+919XXXXXXS3", "Third Msg");
// Thread.sleep(1000);
// gsm.dial("9XXXXS773");
// gsm.dial("871XXXXS5");
// Thread.sleep(1000);
// gsm.welcomeMessage();
// Thread.sleep(1000);
// gsm.welcomeMessage();// for turning on Echo ATE1&W

// Thread.sleep(20000);
// welcomeMessage();

// gsm.hangup();
// Thread.sleep(1000);
// gsm.closePort();
// gsm.outCommand();
// System.exit(1);

} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Can't init this card");
}
}
}

最佳答案

如果没有花费大量时间来重现您的环境,我可以给您两个建议,告诉您如何看待这个问题。

a) 在整个处理程序周围放置一个 try-catch Throwable——通常抛出异常会破坏事件调度器

B) 当您进入和退出您的事件处理程序时打印出一条调试语句,并确保它们是配对的,并且您在最后一次进入后获得退出——线程锁定有时会阻止 future 的事件被调度。

顺便说一下,你在一个地方有一个空的 catch 语句——那些把我吓坏了,因为它们经常以可能浪费你几天时间的方式掩盖问题。如果你绝对知道你想在那里静静地吃掉一个异常,并且你希望这个异常发生到足以污染你的日志来记录它,请发表评论这样说。

关于用于带有 GSM 调制解调器 rxtx 的 IVRS 的 Java 多线程(播放语音文件使事件监听器停止工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32338134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com