Skip to main content
gamedev.net
🔒 Locked

[java] JavaSound

Started by ServantOfGlaaki Apr 8, 2002 at 12:30 PM 2 replies 450+ views
Original Post
ServantOfGlaaki
ServantOfGlaaki
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!
  
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;
	}
}
  
And the other one...
  
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 );
	}
}
  
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]
And a woman needs a man... like a fish needs a bicycle...[U2 - Tryin' to throw your arms around the world]
HenryApe
HenryApe
You can try wrapping the input stream in a BufferedInputStream and maybe try increasing the buffer size if that does not work.
Wrathnut
Wrathnut
This might be stupid but... I didn''t notice any mork or reset method''s in either class so I am assuming the error is coming from the class that implements playable. I''ve never made a class with this type of interface before but I am guessing that you might have to atleast override these methods in that class, since they are obviously being sought by the vm.

-Just when you think things are starting to look up, life grabs you by the jaws makes you open up wide and sh*ts down your throat.
HenryApe
HenryApe
Mark and reset are optional stream operations. Some streams might not support them, but the API doc for the BufferedInputStream states that markSupported() always returns true for that class. Wrapping (chaining) the stream that does not support mark/reset inside a BufferedInputStream should give you the mark functionality that the class wants to use.

In other words, mark and reset are already overridden in BufferedInputStream so no need to do it yourself.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.