音频文件在Eclipse IDE中播放;但不是JAR文件 [英] Audio file plays in Eclipse IDE; but NOT as JAR file

查看:153
本文介绍了音频文件在Eclipse IDE中播放;但不是JAR文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的音频文件位于此处: http:// www .orangefreesounds.com / loud-alarm-clock-sound /

The audio file that I am using is found here: http://www.orangefreesounds.com/loud-alarm-clock-sound/

这是我的Eclipse IDE中的文件结构:
< img src =https://i.stack.imgur.com/iWFZw.pngalt =我的文件结构如何>

This is what my file structure looks like in my Eclipse IDE:

音频文件播放当我在我的IDE中运行它完全正常,但是当我将其导出为JAR文件时不是这样。我已经检查并发现音频文件在JAR文件内。

The audio file plays perfectly fine when I run it in my IDE, but not when I export it as a JAR file. I have already checked and found that the audio file is inside the JAR file.

我正在使用终端命令 java -jar Sandbox.jar& 来运行JAR文件。该程序似乎能够找到文件(因为它没有抛出IOException),但似乎无法执行播放。

I am using the terminal command java -jar Sandbox.jar & to run the JAR file. The program seems to be able to find the file (since it is not throwing an IOException), but does not seem to be able to perform playback.

为什么这个问题发生,我该如何解决?

Why is this problem happening and how can I fix it?

好的,实际上,JAR文件可以在Windows 8.1上运行 cmd PowerShell 时播放音频文件,但在Ubuntu 14.04终端中一些原因。这一次,我一直在尝试在Ubuntu 14.04中运行JAR文件。

Okay, so actually, the JAR file is able to play the audio file when run in cmd or PowerShell on Windows 8.1, but not in the terminal of Ubuntu 14.04 for some reason. This whole time, I have been trying to run the JAR file in Ubuntu 14.04.

我已经确认JAR文件的问题只在一个 Windows 8.1 系统上工作。这个问题中的两个代码片段都不能工作,而MadProgrammer的解决方案都可以工作。

I have confirmed the issue of the JAR files only working on a Windows 8.1 system. Both of the code snippets in this question DO NOT WORK, while both of MadProgrammer's solutions work.

import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.*;

public class Sandbox
{

    public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException
    {
        URL url = Sandbox.class.getResource("/sound-effects/alarmSoundClip.wav");
        AudioInputStream ais = AudioSystem.getAudioInputStream(url);
        AudioFormat af = ais.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, af);

        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(ais);
        clip.start();
    }

}



尝试解决方案#1在Windows或Ubuntu上工作)



一个尝试的解决方案(由Andrew Thompson建议)写入 this.getClass()。getResource(.. 。)而不是Sandbox.class.getResource(...):

Attempted Solution #1 (does NOT work on Windows or Ubuntu)

One attempted solution (as suggested by Andrew Thompson) was to write this.getClass().getResource( ... ) instead of Sandbox.class.getResource( ... ):

import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.*;

public class Sandbox
{

    public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException
    {
        new Sandbox();
    }

    public Sandbox() throws UnsupportedAudioFileException, IOException, LineUnavailableException
    {
        URL url = this.getClass().getResource("/sound-effects/alarmSoundClip.wav");
        AudioInputStream ais = AudioSystem.getAudioInputStream(url);
        AudioFormat af = ais.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, af);

        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(ais);
        clip.start();
    }
}


推荐答案

添加 剪辑。 clip.start()之后的.drain() 似乎对我有用(IDE和命令行都有和没有&

Adding clip.drain() after clip.start() seems to have worked okay for me (IDE and command line both with and without &)

import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Sandbox {

    public static void main(String[] args)  {
        try {
            URL url = Sandbox.class.getResource("/sound-effects/Loud-alarm-clock-sound.wav");
            AudioInputStream ais = AudioSystem.getAudioInputStream(url);
            AudioFormat af = ais.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, af);

            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(ais);
            clip.start();
            System.out.println("Drain...");
            clip.drain();
            System.out.println("...Drained");
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException exp) {
            exp.printStackTrace();
        }
    }

}

说过,我发现过去一点点不太可靠,特别是当有多个声音播放时,我倾向于使用一个 LineListener, drain

Now, having said that, I have found drain a little unreliable in the past, especially when there are multiple sounds playing in which case I tend to use a LineListener

例如...

import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Sandbox {

    protected static final Object LOCK = new Object();

    public static void main(String[] args) {
        try {
            URL url = Sandbox.class.getResource("/sound-effects/Loud-alarm-clock-sound.wav");
            AudioInputStream ais = AudioSystem.getAudioInputStream(url);
            AudioFormat af = ais.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, af);

            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(ais);

            clip.addLineListener(new LineListener() {
                @Override
                public void update(LineEvent event) {
                    System.out.println(event.getType());
                    if (event.getType() == LineEvent.Type.STOP) {
                        synchronized (LOCK) {
                            LOCK.notify();
                        }
                    }
                }
            });
            clip.start();

            synchronized (LOCK) {
                LOCK.wait();
            }
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException | InterruptedException exp) {
            exp.printStackTrace();
        }
    }

}

这篇关于音频文件在Eclipse IDE中播放;但不是JAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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