Posted November 21, 201410 yr 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.
November 22, 201410 yr Author It appears I need to get the file name to put in resourceLocation. The music items for my mod are added via a config and linked to a resourcepack .json file. Anyone know how to get the file name so I can path to it?
November 22, 201410 yr Author 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...
November 22, 201410 yr Author 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?
November 22, 201410 yr Author I may have found a way digging deep in the minecraft sound files to get the length from an input stream. But as per my original post I can't seem to get an input stream returned from a resource.
November 24, 201410 yr Author 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(); }
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.