Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me how I can able to play song using Java. I want to play a part of the song, but it gives an error:

public class Song implements LineListener {

     

             

    /**

         * this flag indicates whether the playback completes or not.

         */

        boolean playCompleted;

         

        /**

         * Play a given audio file.

         * @param audioFilePath Path of the audio file.

         */

     

    

    void playFrame(String songName) {

        

    File audioFile = new File(songName);

         

    try {

             AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

  

             AudioFormat format = audioStream.getFormat();

  

             DataLine.Info info = new DataLine.Info(Clip.class, format);

  

             Clip audioClip = (Clip) AudioSystem.getLine(info);

  

             audioClip.addLineListener(this);

  

             audioClip.open(audioStream);

             

             int nrFrames = audioClip.getFrameLength();

             

             audioClip.setLoopPoints(nrFrames/3, nrFrames/3*2);

              

             audioClip.start();

              

             while (!playCompleted) {

                 // wait for the playback completes

                 try {

                     Thread.sleep(1000);

                 } catch (InterruptedException ex) {

                     ex.printStackTrace();

                 }

             }

              

             audioClip.close();

              

         } catch (UnsupportedAudioFileException ex) {

             System.out.println("The specified audio file is not supported.");

             ex.printStackTrace();

         } catch (LineUnavailableException ex) {

             System.out.println("Audio line for playing back is unavailable.");

             ex.printStackTrace();

         } catch (IOException ex) {

             System.out.println("Error playing the audio file.");

             ex.printStackTrace();

         }

}

    @Override

       public void update(LineEvent event) {

    LineEvent.Type type = event.getType();

     

    if (type == LineEvent.Type.START) {

        System.out.println("Playback started.");

        

    } else if (type == LineEvent.Type.STOP) {

        playCompleted = true;

        System.out.println("Playback completed.");

    }

}

     

   

    public static void main(String[] args)  {

        

        String fileName1 = "song1.wav";

String fileName2 = "song2.wav";

        Song player = new Song();

        player.playFrame(fileName1);

        

        Song player2 = new Song();

        player2.playFrame(fileName2);

    }

 

}

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

Basically, you have initiated the loop, but make sure you have run the loop. And use .loop(int count) method instead of using .start() method. 

I hope this will help.

Want to become a Java Expert? Join Java Course now!!

Want to know more about Java? Watch this video on Java Tutorial for Beginners | Java Programming:

Browse Categories

...