sound not playing in jar

前端 未结 5 1095
天命终不由人
天命终不由人 2020-12-22 12:37

I have packed all the class files and resources as a jar but on execution the sound files wont play. My package structure is:

+project
|_classes
|_ _*.class
         


        
5条回答
  •  暖寄归人
    2020-12-22 12:47

    An alternate theory to those already presented. Often successful getResource() calls depend on the class loader instance that is called to locate them. For this reason, I would recommend to use an instance of a user defined object, from which to call getResource(). E.G.

    // Sanity check
    System.out.println("The value of 'file' is: " + file);
    // Presuming kidsClassRoom1 is an instance of kidsClassRoom
    AudioInputStream inputStream = AudioSystem.
        getAudioInputStream(
            kidsClassRoom1.
                getClass().
                getResourceAsStream("/resources/"+file));
    

    You might also note that snippet uses the prefix of "/" for the resource. Contrary to what others are saying, I am confident that means 'from the root' of the resource path, in whatever Jar on the run-time class-path it is found. Leaving the '/' or '../' out will have the class loader searching for the resource in a sub-path of the class that this is occurring in.

    Of course - make sure the Wav ends up in the Jar! Copy/rename the .jar to a .zip and double click it is the 'quick & dirty' way to examine the archive contents on Windows.

提交回复
热议问题