Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:50:49+00:00 2026-06-01T10:50:49+00:00

I am using Clip in java to play a song as follows: MR.clip= (Clip)

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:50:51+00:00Added an answer on June 1, 2026 at 10:50 am

    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 cache byte[]s and construct an AudioInputStream per-playback. This is because AudioInputStream is 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.

    public class CachedSoundClipTest
    {
        static ArrayList<AudioInputStream> cachedSounds = 
            new ArrayList<AudioInputStream>();
    
        public static void main(String[] args) throws Exception
        {
            File[] audioFiles = new File("/audio_storage_directory").listFiles();
            for (File file : audioFiles)
            {
                AudioInputStream reusableAudioInputStream = 
                    createReusableAudioInputStream(file);
                cachedSounds.add(reusableAudioInputStream);
            }
    
            while(true)
            {
                System.out.println("Press enter to play next clip");
                BufferedReader br = 
                    new BufferedReader(new InputStreamReader(System.in));
                br.readLine();
                playCachedSound(0);
            }
        }
    
        private static void playCachedSound(int i) 
            throws IOException, LineUnavailableException
        {
            AudioInputStream stream = cachedSounds.get(i);
            stream.reset();
            Clip clip = AudioSystem.getClip();
            clip.open(stream);
            clip.start();
        }
    
        private static AudioInputStream createReusableAudioInputStream(File file) 
            throws IOException, UnsupportedAudioFileException
        {
            AudioInputStream ais = null;
            try
            {
                ais = AudioSystem.getAudioInputStream(file);
                byte[] buffer = new byte[1024 * 32];
                int read = 0;
                ByteArrayOutputStream baos = 
                    new ByteArrayOutputStream(buffer.length);
                while ((read = ais.read(buffer, 0, buffer.length)) != -1)
                {
                    baos.write(buffer, 0, read);
                }
                AudioInputStream reusableAis = 
                    new AudioInputStream(
                            new ByteArrayInputStream(baos.toByteArray()),
                            ais.getFormat(),
                            AudioSystem.NOT_SPECIFIED);
                return reusableAis;
            }
            finally
            {
                if (ais != null)
                {
                    ais.close();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem playing a sound correctly in Java using the Clip interface.
I am using the following code to play a sound file using the java
I want to play a .wav file using java code which is in a
So I'm creating a movie clip using flash... Is there any way to start
I'm simply trying to play a sound clip using the javax.sound.sampled library, using the
I'm using the following code to try to play a sound clip with the
I'm using Java sound to play back a number of sound samples with the
I am trying to play an audio clip (using AVAudioPlayer) and a video clip
I have a clip that catches the click event using on(release) { .... }
I am using Radeditor. i need to get the content from clip board and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.