Java 获取要在JAR文件中播放的音频

Java 获取要在JAR文件中播放的音频,java,swing,Java,Swing,我知道这个问题已经被问过很多次了,我也看了很多,希望能找出我的问题,但这并没有起到多大的作用。我被赋予这个类来在我的GUI上播放WAV文件,它在检索本地文件时起作用。但它在JAR文件中不起作用。我知道这是因为music类只用于检索本地文件,所以我需要帮助更改代码以从JAR检索音乐文件 以下是SoundPlayer课程: package extra; import java.io.IOException; import java.util.HashMap; import java.util.Ma

我知道这个问题已经被问过很多次了,我也看了很多,希望能找出我的问题,但这并没有起到多大的作用。我被赋予这个类来在我的GUI上播放WAV文件,它在检索本地文件时起作用。但它在JAR文件中不起作用。我知道这是因为music类只用于检索本地文件,所以我需要帮助更改代码以从JAR检索音乐文件

以下是SoundPlayer课程:

package extra;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.pscode.xui.sound.bigclip.BigClip;

/**
 * A class to play audio clips. Caches previously-played clips,
 * allowing fast re-playback of previously played sounds.
 * 
 * @author Anon
 * @version 1.51
 */

public class SoundPlayer {

/** A cache of previously-played audio clips. */
private final Map<String, Clip> myClips = new HashMap<String, Clip>();

/**
 * Plays the audio file with the given file name.
 * This method returns instantly, without waiting for the clip to finish playing.
 * 
 * @param theFilename The name of the file to play.
 * @return a Clip object representing the sound played.
 * @throws IllegalArgumentException if there is a problem reading from the sound file.
 */
public Clip play(final String theFilename) throws IllegalArgumentException {
    return loop(theFilename, 1);
}

/** 
 * Plays the clip with the given file name in a continuous loop.
 * The clip keeps looping until it is later stopped by calling the 
 * stop() method. This function returns instantly
 *    
 * @param theFilename The name of the file to play.
 * @return a Clip object representing the sound played.
 * @throws IllegalArgumentException if there is a problem reading from the sound file.
 */
public Clip loop(final String theFilename) throws IllegalArgumentException {
    return loop(theFilename, Clip.LOOP_CONTINUOUSLY);
}

/** 
 * Plays the clip with the given file name in a loop.
 * The clip loops until it has played the specified number of times,
 * or until it is later stopped by calling the stop() method.
 * This function returns instantly, without waiting for the clip to finish looping.
 *
 * @param theFilename The name of the file to play.
 * @param theNumberOfTimes The number of times to loop the clip.
 * @return a Clip object representing the sound played.
 * @exception IllegalArgumentException if there is a problem reading from the sound file.
 */
public Clip loop(final String theFilename, final int theNumberOfTimes) 
    throws IllegalArgumentException {

    final Clip clip = getClip(theFilename);

    if (clip != null) {
        clip.loop(theNumberOfTimes);
    }

    return clip;
}

/**
 * Pauses the clip with the given file name.
 * If the clip is later played, it will resume from where it was paused.
 * Calling this method does not resume a thread that is 
 * suspended on a playAndWait() or a loopAndWait().
 * 
 * If stop() is called on a paused clip, it will reset to the
 * beginning of the clip for the next play.
 * 
 * @param theFilename The name of the file to pause.
 * @exception IllegalArgumentException if there is a problem reading from
 * or playing the sound file.
 */
public void pause(final String theFilename) throws IllegalArgumentException {
    final Clip clip = getClip(theFilename);

    if (clip != null) {
        final int pos = clip.getFramePosition();
        clip.stop();
        clip.setFramePosition(pos);
    }
}

/** 
 * Stops the clip with the specified filename
 * (and wakes up any threads waiting for it to finish playing).
 * 
 * @param theFilename The name of the file to stop.
 * @return a Clip object representing the sound stopped.
 * @exception IllegalArgumentException if there is a problem reading from the sound file.
 */
public Clip stop(final String theFilename) 
    throws IllegalArgumentException {
    final Clip clip = getClip(theFilename);
    stopClip(clip);

    return clip;
}

/** 
 * Stops all currently playing sound clips
 * (and wakes up the threads waiting for them to finish playing).
 */
public void stopAll() {
    for (final Clip clip : myClips.values()) {
        stopClip(clip);
    }
}   

/** 
 * Preloads the clip at the given file name.
 * This means the clip will be available faster, when requested for playing the first time.
 * @param theFilename The name of the file to preload.
 * @return a Clip object representing the preloaded sound.
 * @exception IllegalArgumentException if there is a problem reading from the sound file.
 */
public Clip preLoad(final String theFilename) 
    throws IllegalArgumentException {
    return getClip(theFilename);
}


/**
 * Returns a Clip object for a filename, either by creating
 * a new one or loading it from the cache.
 * 
 * @param theFilename The name of the file to load.
 * @return a Clip object, or null if one is not found.
 * @exception IllegalArgumentException if there is a problem reading from the sound file.
 */
private Clip getClip(final String theFilename) throws IllegalArgumentException {
    BigClip clip = null;
    AudioInputStream ais = null;

    if (myClips.containsKey(theFilename)) {
        clip = (BigClip) myClips.get(theFilename);
    } else {
        // read audio file from disk
        try {
            ais = AudioSystem.getAudioInputStream(new File(theFilename));


            clip = new BigClip();
            clip.open(ais);
            clip.addLineListener(new LineListener() {
                /** 
                 * Responds to audio events generated by clips. 
                 * 
                 * @param theEvent The event generated.
                 */
                public void update(final LineEvent theEvent) {
                    if (theEvent.getType() == LineEvent.Type.STOP) {
                        // clip is done playing
                        stopClip((Clip) theEvent.getSource());
                    }
                }
            });
            myClips.put(theFilename, clip);
        } catch (final UnsupportedAudioFileException uafe) {
            throw new IllegalArgumentException
            ("Not a valid supported audio file: \"" + theFilename + "\"", uafe);
        } catch (final LineUnavailableException lue) {
            lue.printStackTrace();
            throw new IllegalArgumentException
            ("Line is not available to play sound \"" + theFilename + " \"", lue);
        } catch (final IOException ioe) {
            throw new IllegalArgumentException
            ("I/O error while reading file: \"" + theFilename + "\" ", ioe);
        }
    }

    return clip;
}

/**
 * Stops the playing of the specified clip.
 * 
 * @param theClip The clip.
 */
private void stopClip(final Clip theClip) {
    if (theClip != null) {
        synchronized (theClip) {
            theClip.stop();
            theClip.setFramePosition(0);
            theClip.notifyAll();  // awaken threads waiting for this Clip
        }
    }
}
}

// end of class SoundPlayer
额外包装;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
导入javax.sound.sampled.AudioInputStream;
导入javax.sound.sampled.AudioSystem;
导入javax.sound.sampled.Clip;
导入javax.sound.sampled.LineEvent;
导入javax.sound.sampled.LineListener;
导入javax.sound.sampled.LineUnavailableException;
导入javax.sound.sampled.unsupportDaudioFileException;
导入org.pscode.xui.sound.bigclip.bigclip;
/**
*播放音频剪辑的类。缓存以前播放的剪辑,
*允许快速重新播放以前播放的声音。
* 
*@作者Anon
*@version 1.51
*/
公共级声音播放器{
/**以前播放的音频剪辑的缓存*/
私有最终映射myClips=newhashmap();
/**
*播放具有给定文件名的音频文件。
*此方法立即返回,而无需等待剪辑完成播放。
* 
*@param theFilename要播放的文件的名称。
*@返回表示播放声音的剪辑对象。
*如果读取声音文件时出现问题,@将引发IllegalArgumentException。
*/
公共剪辑播放(文件名的最后一个字符串)引发IllegalArgumentException{
返回循环(文件名,1);
}
/** 
*在连续循环中播放具有给定文件名的剪辑。
*剪辑会一直循环,直到稍后通过调用
*stop()方法。此函数立即返回
*    
*@param theFilename要播放的文件的名称。
*@返回表示播放声音的剪辑对象。
*如果读取声音文件时出现问题,@将引发IllegalArgumentException。
*/
公共剪辑循环(文件名的最后一个字符串)引发IllegalArgumentException{
返回循环(文件名、Clip.loop\u);
}
/** 
*在循环中播放具有给定文件名的剪辑。
*剪辑将循环播放,直到播放指定次数,
*或者直到稍后通过调用stop()方法停止。
*此函数立即返回,无需等待剪辑完成循环。
*
*@param theFilename要播放的文件的名称。
*@param theNumberOfTimes循环剪辑的次数。
*@返回表示播放声音的剪辑对象。
*@exception IllegalArgumentException如果读取声音文件时出现问题。
*/
公共剪辑循环(文件名的最终字符串、最终整数和次数)
抛出IllegalArgumentException{
最终剪辑剪辑=getClip(文件名);
if(clip!=null){
clip.loop(循环次数);
}
回程夹;
}
/**
*暂停具有给定文件名的剪辑。
*如果稍后播放该剪辑,它将从暂停处继续播放。
*调用此方法不会恢复正在运行的线程
*在playAndWait()或loopAndWait()上挂起。
* 
*如果对暂停的剪辑调用stop(),它将重置为
*下一个剧本剪辑的开头。
* 
*@param theFilename要暂停的文件的名称。
*@exception IllegalArgumentException如果从中读取时出现问题
*或者播放声音文件。
*/
public void pause(文件名的最后一个字符串)引发IllegalArgumentException{
最终剪辑剪辑=getClip(文件名);
if(clip!=null){
final int pos=clip.getFramePosition();
clip.stop();
夹子。设置框架位置(位置);
}
}
/** 
*使用指定的文件名停止剪辑
*(并唤醒等待它完成播放的所有线程)。
* 
*@param theFilename要停止的文件的名称。
*@返回表示声音停止的剪辑对象。
*@exception IllegalArgumentException如果读取声音文件时出现问题。
*/
公共剪辑停止(文件名的最终字符串)
抛出IllegalArgumentException{
最终剪辑剪辑=getClip(文件名);
止动夹(夹);
回程夹;
}
/** 
*停止当前播放的所有声音片段
*(并唤醒等待它们完成播放的线程)。
*/
公开作废stopAll(){
对于(最终剪辑:myClips.values()){
止动夹(夹);
}
}   
/** 
*在给定文件名处预加载剪辑。
*这意味着在第一次播放请求时,该剪辑将更快可用。
*@param theFilename要预加载的文件的名称。
*@返回表示预加载声音的剪辑对象。
*@exception IllegalArgumentException如果读取声音文件时出现问题。
*/
公共剪辑预加载(文件名的最终字符串)
抛出IllegalArgumentException{
返回getClip(文件名);
}
/**
*通过创建
*一个新的或从缓存加载它。
* 
*@param theFilename要加载的文件的名称。
*@返回一个剪辑对象,如果找不到,则返回null。
*@exception IllegalArgumentException如果读取声音文件时出现问题。
*/
私有剪辑getClip(文件名的最后一个字符串)引发IllegalArgumentException{
BigClip=null;
音频输入流ais=null;
if(myClips.containsKey(文件名)){
clip=(BigClip)myClips.get(文件名);
}否则{
//从磁盘读取音频文件
试一试{
ais=AudioSystem.getAudioInputStream(新文件(文件名));
clip=新的BigClip();
夹子。打开(ais);
clip.addLineListener(新的LineListener(){
/** 
*响应剪辑生成的音频事件。
* 
*@param事件已生成。
*/
公共作废更新(最终行事件){