Original Post
Hi there. I''ve just started playing around with JavaSound. I wrote the code you''ll find at the bottom of this post, but... it doesn''t work. Something about not being able to reset the InputStream. Anyways... here''s the code... Please help me!
And the other one...
The error message I get when I run MediaManager.class is ''mark/reset not supported'', and it seems that it''s the reset it doesn''t like.
It''''s over for now... until yesterday begins again... tomorrow... tomorrow... tomorrow...
[Powerman 5000 - Watch the Sky for Me]
import java.io.InputStream;
import java.io.IOException;
import javax.sound.sampled.*;
public class SoundClip implements Playable
{
private AudioInputStream m_ais = null;
private AudioFormat m_af = null;
private DataLine.Info m_info = null;
private SoundClip( AudioInputStream ais, AudioFormat af, DataLine.Info info )
{
m_ais = ais;
m_af = af;
m_info = info;
}
public void play( ) throws SoundException
{
try
{
SourceDataLine sdl = ( SourceDataLine ) AudioSystem.getLine(m_info);
sdl.open( m_af );
sdl.start( );
int bufferSize = ( int ) m_af.getSampleRate( ) *
m_af.getFrameSize( );
byte [] buffer = new byte [ bufferSize ];
try
{
int bytesRead = 0;
while ( bytesRead >= 0 )
{
bytesRead = m_ais.read ( buffer,
0,
buffer.length );
if ( bytesRead >= 0 )
sdl.write( buffer, 0, bytesRead );
}
}
catch ( IOException e )
{
throw new SoundException("IOException: Sound Error");
}
sdl.drain();
sdl.close();
}
catch ( LineUnavailableException e )
{
throw new SoundException( "LineUnavailableException: Sound Error" );
}
}
public void loop( )
{}
public void stop( )
{}
public static SoundClip loadClip(InputStream is) throws SoundException
{
SoundClip m_returnClip = null;
AudioInputStream ais = null;
AudioFormat af = null;
DataLine.Info info = null;
try
{
ais = AudioSystem.getAudioInputStream( is );
af = ais.getFormat();
info = new DataLine.Info ( SourceDataLine.class, af );
}
catch(UnsupportedAudioFileException e1)
{
throw new SoundException("UnsupportedAudioFileException: Sound Error");
}
catch(IOException e2)
{
System.out.println(e2.getMessage());
e2.printStackTrace();
throw new SoundException("IOException: Sound Error");
}
m_returnClip = new SoundClip( ais, af, info );
return m_returnClip;
}
}
import java.util.zip.*;
import java.io.*;
public class MediaManager
{
public MediaManager( )
{}
public SoundClip getSoundClip( InputStream is )
{
SoundClip clip = null;
try
{
clip = SoundClip.loadClip( is );
}
catch( SoundException se )
{
System.out.println( se.getMessage( ) );
}
if(clip == null)
return null;
else
return clip;
}
public static void main( String[] args ) throws Exception
{
MediaManager mm = new MediaManager( );
ZipFile zf = new ZipFile("kiss.zip");
ZipEntry ze = zf.getEntry("Kiss my Royal Irish.wav");
SoundClip clip = mm.getSoundClip(zf.getInputStream ( ze ) );
clip.play( );
System.exit( 0 );
}
}