当我从.jar运行时,为什么Java Sound会有不同的表现? [英] Why does Java Sound behave differently when I run from a .jar?

查看:70
本文介绍了当我从.jar运行时,为什么Java Sound会有不同的表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的play方法来自一个类,该类在实例化时将.wav文件读取到名为data的字节数组中,并将声音格式存储在名为formatAudioFormat对象中.

我有一个从java.util.Timer调用play的程序.当我进入包含所有相关.class文件的文件夹,并使用命令java MainClass运行该程序时,一切都会按预期进行.但是,当我将所有.class文件放在可执行文件.jar中并使用命令java -jar MyProgram.jar运行该程序时,使用play方法播放的声音会在大约50到150毫秒后被切断.

public void play() throws LineUnavailableException {
    final Clip clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, format));
    clip.open(format, data, 0, data.length);
    new Thread() {
        public void run() {
            clip.start();
            try {
                Thread.sleep(300); // all sounds are less than 300 ms long
            } catch (InterruptedException ex) { /* i know, i know... */ }
            clip.close();
        }
    }.start();
}

一些评论:

  • 我尝试将play方法中的睡眠时间增加到1000 ms,而行为没有变化.

  • 使用System.nanoTime计时Thread.sleep可以确认线程正在完全按照预期的时间休眠.

  • 由于要播放的声音文件已预先加载到内存中,所以我认为从.jar中提取声音资源的行为不会引起问题.

  • 我尝试使用内存池大小选项-Xms2m-Xmx64m(分别)从jar的内部和外部运行该程序,而没有改变行为.

我正在Ubuntu 11.04上运行OpenJDK Java 6.知道发生了什么事吗?

解决方案

问题很可能是byte[]的加载.

The play method below is from a class which, upon instantiation, reads a .wav file into a byte array called data, and stores the sound format in an AudioFormat object called format.

I have a program that calls play from a java.util.Timer. When I go into the folder with all the relevant .class files and I run the program using the command java MainClass, everything works as expected. However, when I put all the .class files in an executable .jar and run the program using the command java -jar MyProgram.jar, sounds played using the play method are cut off after something like 50 to 150 ms.

public void play() throws LineUnavailableException {
    final Clip clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, format));
    clip.open(format, data, 0, data.length);
    new Thread() {
        public void run() {
            clip.start();
            try {
                Thread.sleep(300); // all sounds are less than 300 ms long
            } catch (InterruptedException ex) { /* i know, i know... */ }
            clip.close();
        }
    }.start();
}

A few comments:

  • I've tried increasing the sleep time in the play method up to 1000 ms, with no change in behavior.

  • Timing the Thread.sleep using System.nanoTime confirms that the thread is sleeping exactly as long as expected.

  • Since the sound file to be played is pre-loaded into memory, I don't think the act of extracting the sound resource from the .jar can be causing the problem.

  • I've tried running the program from both inside and outside the jar using the memory pool size options -Xms2m and -Xmx64m (separately), with no change in behavior.

I'm running OpenJDK Java 6 on Ubuntu 11.04. Any idea what's going on?

解决方案

It is likely the loading of the byte[] that is the problem.

这篇关于当我从.jar运行时,为什么Java Sound会有不同的表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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