Hi,
Firstly great work on kbmod
I seem to have a problem though. I want load my .mod’s from their own .jar. This is so I can update my game code (in another .jar) whenever I like and users don’t need to redownload the music files.
I have written some code to perform this, but for some reason I always have a corrupt inputstream, the music plays ok to begin with (just), then gets worse.
Any ideas where i’m going wrong on this (code below)?
Ideally, however, I would like to use some other .mod code that I have already within my game (it’ll be a nightmare to switch to kbmod at this point). This, however, uses RandomAccessFile for loading the .mod, not streams.
I understand from trying to do this all day that I can’t use that directly as the file is in .jar, and a seperate one from the playback code, at that, so streams, I was informed was the only way to go. Then I found kbmod and tried to get it working.
Is there an easy way to make JNLP work with RandomAccessFile at all? Somebody suggested extracting the file on execution and using that, but I don’t see an easy way to do that.
Is there something obvious i’m overlooking for using RandomAccessFiles within JNLP jar’s?
On a side note I gave a shareware JNLP creator a whirl and it seemed to fix up my original (definately non-JNLP compatible - playing the .mods from the same .jar, however) code somehow. Still a big step ahead of me (who currently sees no easy way to play .mods with JNLP/JARs).
public class Player {
JarFile jar = null;
InputStream entryStream = null;
Module m = null;
SourceDataLine line = null;
JarEntry entry = null;
public Player()
{
PlayStream();
}
public void PlayStream()
{
try
{
m = new Module(this.load());
}
catch (Exception e)
{}
KBMod player = new KBMod( m );
System.out.println( " Song Name : " + m.name );
// Allocate the output buffers.
float[] lBuf = new float[3600];
float[] rBuf = new float[3600];
byte[] output = new byte[14400];
AudioFormat format = new AudioFormat( 44100, 16, 2, true, false );
DataLine.Info lineInfo = new DataLine.Info( SourceDataLine.class, format );
try
{
line = (SourceDataLine)AudioSystem.getLine(lineInfo);
line.open( format );
line.start();
}
catch (Exception e)
{
}
while( true )
{
int len = player.getAudio( lBuf, rBuf );
int outpos = 0;
for( int n=0; n<len; n++ )
{
short l = (short)( lBuf[n]*32767 );
short r = (short)( rBuf[n]*32767 );
lBuf[n] = 0;
rBuf[n] = 0;
output[outpos++] = (byte)(l&0xFF);
output[outpos++] = (byte)(l>>8);
output[outpos++] = (byte)(r&0xFF);
output[outpos++] = (byte)(r>>8);
}
line.write( output, 0, len<<2 );
}
}
public InputStream load()
{
try
{
jar = new JarFile("Music.jar");
}
catch (Exception e)
{}
Enumeration entries = jar.entries();
// Get the next entry.
entry = (JarEntry) entries.nextElement();
// Get an input stream for the entry.
try
{
entryStream = jar.getInputStream(entry);
}
catch (Exception e){}
return entryStream;
}
public static void main( String[] args ) throws Exception
{
Player p = new Player();
}
}
kbmod outputs these errors:
Instrument 1 : 903 samples missing!
Instrument 3 : 365 samples missing!