I am using Clip in java to play a song as follows:
MR.clip= (Clip) AudioSystem.getLine(MR.info[docIdOfSelectedSong]);
MR.clip.open(MR.sounds[docIdOfSelectedSong]);
MR.clip.setMicrosecondPosition(5* 1000000);
MR.clip.start();
where MR.sounds is an array of type AudioInputStream and MR.info is an array of type DataLine.info. When I press a button ,the above code is called to play the song. Moreover, I have another button to stop the song which calls the below code
public static void stopSong(){
MR.clip.close();
}
The problem is that when I play the song for the first time, the play and stop button are working fine. But, when I try to play the song for the second time, I cannot hear the song. Any suggestions on what is going wrong?
Like all other InputStreams, AudioInputStream can only be read once (unless it can be .reset()). You could try calling .reset() on the AudioInputStream before attemping to play the sound again, but AudioInputStream may not support .reset(). InputStreams are not required to support reset. Also see markSupported().
If .reset() doesn’t work, consider constructing a new AudioInputStream every time you need to start playing.
UPDATE: I made an example of caching sound data in memory and using Clip to play those sounds. This example utilizes AudioInputStream.reset(). So how can that work? In fact, AudioInputStream does supports reset() if and only if its underlying InputStream supports .reset(). So my example creates an AudioInputStream that is backed by a ByteArrayInputStream. Because ByteArrayInputStream supports reset, these cached AudioInputStreams also support .reset(), which allows them to be reused.
Note that if you are going to be playing any one cached sound concurrently, you should probably not cache
AudioInputStreams, but rather cachebyte[]s and construct anAudioInputStreamper-playback. This is becauseAudioInputStreamis stateful, so passing a single instance of it to two concurrently running clips, or resetting a stream while one clip is playing, will result in state conflict.