简体   繁体   English

Java可执行.jar文件不从外部资源播放MP3文件

[英]Java executable .jar file does not play MP3 file from external resource

I have a Java project in Netbeans which includes a User interface. 我在Netbeans中有一个包含用户界面的Java项目。 From the interface the user can play an MP3 file that is stored in a folder "data" within the projects working directory. 用户可以从界面播放存储在项目工作目录中的“数据”文件夹中的MP3文件。 For playing the file I have a class that creates an MP3 player and makes use of the JLayer.jar. 为了播放文件,我有一个创建MP3播放器并使用JLayer.jar的类。

import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.swing.JFileChooser;
import javazoom.jl.player.Player;


public class MP3Player {


private String filename;
private Player player; 

// a default constructor
public MP3Player() {}

// Constructor takes a given file name 
public MP3Player(String filename) {
    this.filename = filename;
}

public void close() { if (player != null) player.close(); }

// play the JLayerMP3 file to the sound card
public void play() {
    try {
        InputStream fis     = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        player = new Player(bis);
    }
    catch (Exception e) {
        System.out.println("\n Problem in playing: " + filename);
        System.out.println(e);
    }
}

public void play(String mp3filename) {
    try {
        InputStream fis     = new FileInputStream(mp3filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        player = new Player(bis);
    }
    catch (Exception e) {
        System.out.println("Problem in playing: " + mp3filename);
        System.out.println(e);
    }

    // creata a thread to play music in background
    new Thread() {
        public void run() {
            try { player.play(); }
            catch (Exception e) { System.out.println(e); }
        }
    }.start();
}

Within the UI class I have a play button with an action method within which I create an MP3 player object. 在UI类中,我有一个带有动作方法的播放按钮,我在其中创建一个MP3播放器对象。 I then pass the filepath to the MP3 Player 然后我将文件路径传递给MP3播放器

MP3Player mp3 = new MP3Player();  
mp3.play("data/audio/" + filepath);

As long as I run this project in Netbeans, it works fine and the music plays. 只要我在Netbeans中运行这个项目,它就能正常工作并播放音乐。 But once I create a jar file it does not play the music. 但是一旦我创建了一个jar文件就不会播放音乐了。 I found some other posts about similar problems For example: Loading an mp3 file with JLayer from inside the Jar 我发现了一些关于类似问题的其他帖子例如: 从Jar内部加载带有JLayer的mp3文件

But contrary to them I don't want to include the MP3 files in the JAR. 但与他们相反,我不想在JAR中包含MP3文件。 It should take the files from a local folder "data". 它应该从本地文件夹“data”获取文件。

I place the "data" folder containing the mp3.files in the same directory as the jar file. 我将包含mp3.files的“data”文件夹放在与jar文件相同的目录中。 So the filepath is exactly the same as when I have it in the Netbeans project's working directory and run it from Netbeans (as I said there is no problem then). 因此,文件路径与我在Netbeans项目的工作目录中使用它时完全相同,并从Netbeans运行它(因为我说没有问题)。 And the JAR gets all the other data like textfiles and images without any problems from the local "data" folder. 并且JAR从本地“data”文件夹中获取所有其他数据,如文本文件和图像,而没有任何问题。

Any help is highly appreciated. 任何帮助都非常感谢。

Netbeans creates dirctory like this: YourProjectDir/dist/YourProject.jar . Netbeans创建了这样的dirctory: YourProjectDir/dist/YourProject.jar Your libraries are placed inside YourProjectDir/dist/lib/otherlibrary.jar . 您的库放在YourProjectDir/dist/lib/otherlibrary.jar Create your data folder inside your YourProjectDir ie your media files will remain inside YourProjectDir/data/audio/ . YourProjectDir创建data文件夹,即您的媒体文件将保留在YourProjectDir/data/audio/

What you do, just create a batch file with any name. 你做什么,只需创建一个任何名称的批处理文件。

@echo off 
START/MAX dist\YourProject.jar

Place the Batch file inside YourProjectDir . 将批处理文件放在YourProjectDir Now run the Batch file. 现在运行批处理文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM