声音不会从jar播放,但只有当我从NetBeans运行它时。 (Javazoom播放器)

时间:2015-11-17 03:54:46

标签: java jar netbeans-8

我已经下载并导入了JLayer 1.0.1 (http://www.javazoom.net/javalayer/sources.html

并使用此代码,如果我从NetBeans运行它,我可以播放声音

package window;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.AdvancedPlayer;

/**
 * Provide basic playing of MP3 files via the javazoom library.
 * See http://www.javazoom.net/
 * 
 * @author David J. Barnes and Michael Kölling
 * @version 2011.07.31
 */
public class MusicPlayer
{
    // The current player. It might be null.
    private AdvancedPlayer player;

    /**
     * Constructor for objects of class MusicFilePlayer
     */
    public MusicPlayer()
    {
        player = null;
    }

    /**
     * Play a part of the given file.
     * The method returns once it has finished playing.
     * @param filename The file to be played.
     */
    public void playSample(String filename)
    {
        try {
            setupPlayer(filename);
            player.play(500);
        }
        catch(JavaLayerException e) {
            reportProblem(filename);
        }
        finally {
            killPlayer();
        }
    }    

    /**
     * Start playing the given audio file.
     * The method returns once the playing has been started.
     * @param filename The file to be played.
     */
    public void startPlaying(final String filename)
    {
        try {
            setupPlayer(filename);
            Thread playerThread = new Thread() {
                public void run()
                {
                    try {
                        player.play(5000);
                    }
                    catch(JavaLayerException e) {
                        reportProblem(filename);
                    }
                    finally {
                        killPlayer();
                    }
                }
            };
            playerThread.start();
        }
        catch (Exception ex) {
            reportProblem(filename);
        }
    }

    public void stop()
    {
        killPlayer();
    }

    /**
     * Set up the player ready to play the given file.
     * @param filename The name of the file to play.
     */
    private void setupPlayer(String filename)
    {
        try {
            InputStream is = getInputStream(filename);
            player = new AdvancedPlayer(is, createAudioDevice());
        }
        catch (IOException e) {
            reportProblem(filename);
            killPlayer();
        }
        catch(JavaLayerException e) {
            reportProblem(filename);
            killPlayer();
        }
    }

    /**
     * Return an InputStream for the given file.
     * @param filename The file to be opened.
     * @throws IOException If the file cannot be opened.
     * @return An input stream for the file.
     */
    private InputStream getInputStream(String filename)
        throws IOException
    {
        return new BufferedInputStream(
                    new FileInputStream(filename));
    }

    /**
     * Create an audio device.
     * @throws JavaLayerException if the device cannot be created.
     * @return An audio device.
     */
    private AudioDevice createAudioDevice()
        throws JavaLayerException
    {
        return FactoryRegistry.systemRegistry().createAudioDevice();
    }

    /**
     * Terminate the player, if there is one.
     */
    private void killPlayer()
    {
        synchronized(this) {
            if(player != null) {
                player.stop();
                player = null;
            }
        }
    }

    /**
     * Report a problem playing the given file.
     * @param filename The file being played.
     */
    private void reportProblem(String filename)
    {
        System.out.println("There was a problem playing: " + filename);
    }

}

我可以从另一个类中调用它

private MusicPlayer player = new MusicPlayer();
player.startPlaying("src/audio/SuperheroKeygen.mp3"); 

音频文件也会导入到项目中。

但如果我运行.jar

,我就没有音频

如果我用Winrar打开.jar文件,我立即得到一个带有我的mp3的文件夹音频,所以mp3包含在.jar

从命令行运行jar给我

  

播放时出现问题:src / audio / SuperheroKeygen.mp3
  线程“Thread-2”中的异常java.lang.NullPointerException
  在window.MusicPlayer $ 1.run(MusicPlayer.java:64)

P.S Java Newbie

1 个答案:

答案 0 :(得分:0)

jar文件可能不是您的播放器可以访问的文件路径。你可以做几件事。您可能需要将文件复制到本地磁盘,如下所示:

Path soundPath = Files.createTempFile("SuperheroKeygen", ".mp3");
try (InputStream jarSound= getClass().getResourceAsStream("/src/audio/SuperheroKeygen.mp3")){
    Files.copy(jarSound, soundPath, StandardCopyOption.REPLACE_EXISTING);        
} catch (IOException e){
    //handle the exception
}

然后soundPath包含您可以传递给播放器的声音文件的路径。

您可能只需更改它,以便在getInputStream方法中,只需使用getClass().getResourceAsStream()获取InputStream而不是创建新FileInputStream 1}}。如果这样做,您必须使用/启动资源路径,以便它从根目录而不是当前程序包目录:

private InputStream getInputStream(String filename) throws IOException {
    return new BufferedInputStream(getClass().getResourceAsStream(filename));
}

您可以使用ClassLoader将所有文件名作为绝对路径读取,因此您不需要像/这样的前导{/ p>:

private InputStream getInputStream(String filename) throws IOException {
    return new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(filename));
}