Closing InputStream after sound file plays?

Hi everybody,

I've been looking on the web and over the forums and I can't seem to find an answer to this (I think I'm missing something silly). I have code in an application that allows a sound to play, but after the sound plays, I'd like to close the InputStream so it isn't left hanging and I can play a different song if I want. The code I have so far is:

import java.io.FileInputStream;

import java.io.InputStream;

import javax.swing.JOptionPane;

import sun.audio.AudioPlayer;

import sun.audio.AudioStream;

publicclass Player{

private AudioStream audioStream;

public Player( String song ){

play( song );

}

publicvoid play( String song ){

try{

InputStream in =new FileInputStream("D:\\Musica\\" + song + ".wav");

audioStream =new AudioStream(in);

AudioPlayer.player.start(audioStream);

}catch( Exception e ){

JOptionPane.showMessageDialog( null,"The music file could not be played.","Error", JOptionPane.PLAIN_MESSAGE );

}

}

}

Can anyone tell me what I have to add? Putting in player.stop() just stops the file right away.

Thanks!

Jezzica85

Message was edited by:

jezzica85

[2115 byte] By [jezzica85a] at [2007-11-27 11:28:41]
# 1

You should use javax.sound.sampled API because sun.audio is an unsupported and undocumented API. Without proper documentation, no one could use it properly let alone give an appropriate answer to your question.

hiwaa at 2007-7-29 16:23:33 > top of Java-index,Java Essentials,Java Programming...