使用rxtx的GSM Modem的Java多线程 IVRS(交互式语音响应系统)问题:播放语音文件时使事件监听器停止工作

14
我已经实现了一个使用GSM调制解调器接收电话的程序。当检测到"RING"时,通过从DATA_AVAILABLE事件处理程序内部调用函数来回答电话并播放音频剪辑。但是,在此之后,事件处理程序停止工作。音频完成后,事件处理程序不再显示任何数据接收事件。
为什么事件监听器停止工作?我是否在事件处理程序内部播放音频时做错了?我正在考虑从data_received事件处理程序内部设置一个变量true或false,并创建自定义事件处理程序来侦听该变量的更改以执行音频播放,这两者是否可以同时工作?
如何创建多线程解决方案,以便串行I/O不会中断,并且可以以同步方式进行音频播放和音频采样以检测DTMF音调。是否有任何方法可以持续监听串行端口事件而不中断,并在特定时间运行音频采样和音频播放功能?
在这种情况下,通过switch和thread启动线程,线程在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");
   }
 }
}

你使用这段代码播放语音文件了吗?当调用playMP3.play()时,我收到了“PORT_UNOWNED”错误。或者语音文件在我的电脑上播放了,而没有发送到客户端。 - delavega66
已经三年了,记不清了。 - codefreaK
明白了,谢谢。 - delavega66
1个回答

0

如果不想花费大量时间来复制您的环境,我可以给您两个建议,让您了解这个问题。

A)在整个处理程序周围放置try-catch Throwable-通常抛出异常可能会破坏事件分派器

B)在进入和退出事件处理程序时打印出调试语句,并确保它们是成对的,并且您在最后一个输入后获得了退出-线程锁定有时会停止未来的事件被分派。

顺便说一下,在某些地方你有一个空捕获语句-那些使我很害怕,因为它们经常掩盖问题,浪费你的时间数天。如果您绝对确定您要在那里默默地吃掉一个异常,并且您期望该异常发生足以污染您的日志记录,请放置一个注释说明。


我已经完成了,但是一旦线程完成,即音频文件被播放完毕,它就会完成执行并执行下面的println,但在此之后不会检测到新事件。至于最后一个空异常部分,我会检查一下。除了隐藏的异常之外,没有其他影响事件监听器,对吧? - codefreaK
你能简化一下代码,让它只有几行就出错吗?把所有的代码都复制下来,然后开始逐步删除,直到你得到大约十几行会出错的代码。此外,你可以尝试不使用新线程进行播放...我相信播放只是将声音排队并返回给你,因此启动一个新线程没有帮助(甚至可能会混淆问题)-但我可能对此有所误解。 - Bill K
最初我所做的是在事件情况下的if条件语句中简单地调用welcomeAudio()函数。由于我对多线程的理解有限,我认为由于函数执行,事件处理程序会在音频文件播放时被某种方式停止,而Bill已经给出了调用音频播放的区域和完整源代码,因此任何拥有GSM调制解调器的人都可以测试我的代码,如果他们打算这样做。 - codefreaK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接