我需要关闭一个音频剪辑? [英] do I need to close an audio Clip?

查看:172
本文介绍了我需要关闭一个音频剪辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个处理的实时数据并假定当一个特定事件发生时发出蜂鸣声的应用程序。该触发事件可以发生每秒多次,并且如果当另一事件触发的code为只是应该忽略它(与中断当前蜂鸣声,并开始一个新的)哔已经播放。这是基本的code:

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 is already playing when another event triggers the code is just supposed to ignore it (as opposed to interrupting the current beep and starting a new one). Here is the basic code:

Clip clickClip

public void prepareProcess() {
    super.prepareProcess();

    clickClip = null;

    try {
        clipFile = new File("C:/WINDOWS/Media/CHIMES.wav");
        ais = AudioSystem.getAudioInputStream(clipFile);
        clickClip = AudioSystem.getClip();
        clickClip.open(ais);

        fileIsLoaded = true;

    } catch (Exception ex) {
        clickClip = null;
        fileIsLoaded = false;
    }
}

public void playSound() {

    if (fileIsLoaded) {

        if ((clickClip==null) || (!clickClip.isRunning())) {

            try {
                clickClip.setFramePosition(0);
                clickClip.start();
            } catch (Exception ex) {
                System.out.println("Cannot play click noise");
                ex.printStackTrace();
            }
        }
    }

的prepareProcess方法得到在开始运行一次,并且playSound方法被调用每一个触发事件发生时的时间。我的问题是:我需要关闭clickClip对象吗?我知道我可以添加一个ActionListener来监视停止活动,但由于事件发生如此频繁,我担心额外的处理是要实时数据采集慢下来。

The prepareProcess method gets run once in the beginning, and the playSound method is called every time a triggering event occurs. My question is: do I need to close the clickClip object? I know I could add an actionListener to monitor for a Stop event, but since the event occurs so frequently I'm worried the extra processing is going to slow down the real-time data collection.

在code似乎运行正常,但我担心的是内存泄漏。在code以上是基于一个例子,我发现同时搜索网,但本例中使用一个ActionListener专门收剪辑,杜绝当停止方法没有得到执行,将发生内存泄漏。我的计划是旨在为小时运行,因此任何内存泄漏我有会引起问题。

The code seems to run fine, but my worry is memory leaks. The code above is based on an example I found while searching the net, but the example used an actionListener to close the Clip specifically "to eliminate memory leaks that would occur when the stop method wasn't implemented". My program is intended to run for hours so any memory leaks I have will cause problems.

我坦率地说,我不知道如何验证是否没有我有一个问题。我使用NetBeans和运行内存分析器只是给了我一个巨大的东西,我不知道如何阅读列表。这应该是程序的简单零件,而我就可以花几个小时。任何帮助将大大AP preciated!

I'll be honest: I have no idea how to verify whether or not I've got a problem. I'm using Netbeans, and running the memory profiler just gave me a huge list of things that I don't know how to read. This is supposed to be the simple part of the program, and I'm spending hours on it. Any help would be greatly appreciated!

迈克尔

推荐答案

是的,结束是必要的。

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

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

在我的应用程序(的util.concurrent来临之前写的),这是剪辑收盘机制。

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

  public static final Vector<Clip> vector = new Vector<Clip>();
  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.

这篇关于我需要关闭一个音频剪辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆