do I need to close an audio Clip?

后端 未结 2 495
北荒
北荒 2021-01-26 21:31

have an application that processes real-time data and is supposed to beep when a certain event occurs. The triggering event can occur multiple times per second, and if the beep

2条回答
  •  长情又很酷
    2021-01-26 22:17

    yes, closing is necessary

      myClip.addLineListener(new LineListener() {
        public void update(LineEvent myLineEvent) {
          if (myLineEvent.getType() == LineEvent.Type.STOP)
            myClip.close();
        }
      });
    

    or by

      if (!myClip.isRunning())
        myClip.close();
    

    In my application (written before the advent of util.concurrent), this is the clip closing mechanism.

      public static final Vector vector = new Vector();
      static final int vector_size = 5;
    
      // typically called before calling play()
      static synchronized void consolidate() {
        while (vector_size < vector.size()) {
          Clip myClip = vector.get(0);
          if (myClip.isRunning())
            break;
          myClip.close();
          vector.remove(0);
        }
        if (vector_size * 2 < vector.size())
          System.out.println("warning: audio consolidation lagging");
      }
    
      public static void play(final File myFile) {
        try {
          AudioInputStream myAudioInputStream = AudioSystem.getAudioInputStream(myFile);
          final Clip myClip = AudioSystem.getClip();
          vector.add(myClip);
          myClip.open(myAudioInputStream);
          myClip.start();
        } catch (Exception myException) {
          myException.printStackTrace();
        }
      }
    

    As one of the comments suggest, it may delay the playback of new clips, but I cannot remember as a few ms delay were not important in my application.

提交回复
热议问题