Jump to content

How to get file name from Resource?


Mctittles

Recommended Posts

I'd like to get the length of a music file.  This is how I currently get the resource for my item:

 

ResourceLocation resourceLocation = currentRecord.getRecordResource("records." + currentRecord.recordName);

 

However that doesn't work in the following:

 

try {
                    AudioInputStream audioInputStream = (AudioInputStream)Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation).getInputStream();
                    AudioFormat format = audioInputStream.getFormat();
                    long frames = audioInputStream.getFrameLength();
                    double durationInSeconds = (frames+0.0) / format.getFrameRate();
                    moreMusic.logger.info("Length in seconds: "+durationInSeconds);
                } catch (Exception ignored) {
                    moreMusic.logger.info("couldn't get the resource for song length: "+ignored);
                }

 

I get the error:  java.io.FileNotFoundException: minecraft:records.record2

 

Does anyone know the correct way to do this?  I do not know the name of the song file because in this mod users add them with resource packs.

Link to comment
Share on other sites

I tried a direct file just to see if the rest of the code would work:

 

try {
            File file = new File("C:/Users/Mctittles/AppData/Roaming/.minecraft/resourcepacks/moreMusic/assets/minecraft/sounds/01 - Intro-.ogg");
            AudioInputStream audioInputStream= AudioSystem.getAudioInputStream(file);
            AudioFormat format = audioInputStream.getFormat();
            long frames = audioInputStream.getFrameLength();
            double durationInSeconds = (frames+0.0) / format.getFrameRate();
            moreMusic.logger.info("Length in seconds: "+durationInSeconds);
        } catch (Exception ignored) {
            moreMusic.logger.info("couldn't get the resource for song length: "+ignored);
        }

 

Unfortunately I get this error:

 

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file

 

So I guess I need two things now.  Getting the file from a resource AND getting the length of an ogg file.  Bummer...

Link to comment
Share on other sites

Thanks!  I'm looking into ogg libraries now.  Would be good if I could just use the minecraft soundsystem to get the song length but I don't see any way to do that.

 

 

a) A resource is not always (most likely not in fact) an actual File. Most of the time it is an entry in a Jar file somewhere. As such you have to work with InputStreams.

 

The files I'm working with will always be in a resource pack (mod is designed to allow you to add music), but either way I don't see how to convert an InputStream into an audio input stream.  Any suggestions?

Link to comment
Share on other sites

So I figured it out.  Here is how to get an input stream from an audio resource:

 

resourceLocation = currentRecord.getRecordResource("records." + myNewRecord.recordName);
SoundEventAccessorComposite sound = Minecraft.getMinecraft().getSoundHandler().getSound(resourceLocation);
resourceLocationFile = sound.func_148720_g().getSoundPoolEntryLocation();
InputStream inputStream = Minecraft.getMinecraft().getResourceManager().getResource(resourceLocationFile).getInputStream();

 

 

Using JAudioTagger I was able to get the song length of an .ogg file.  This isn't ideal for most projects because it requires a file location instead of an inputStream.  Right now it works for me because all my files are in a resourcePack that everyone has a copy of but in the future I'd like to find a way using only the inputStream:

 

resourceLocation = currentRecord.getRecordResource("records." + myNewRecord.recordName);
                    try {
                        SoundEventAccessorComposite sound = Minecraft.getMinecraft().getSoundHandler().getSound(resourceLocation);
                        resourceLocationFile = sound.func_148720_g().getSoundPoolEntryLocation();
                        File soundFile = new File("resourcepacks/moreMusic/assets/minecraft/" + resourceLocationFile.getResourcePath());
                        AudioFile f = AudioFileIO.read(soundFile);
                        AudioHeader audioHeader = f.getAudioHeader();
                        currentLength=audioHeader.getTrackLength();
                        PacketHandler.INSTANCE.sendToServer(new ClientSendsServerGetsSongTimes(slotSent, counter, currentLength, this));
                    } catch (Exception ignored) {
                        ignored.printStackTrace();
                    }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.