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

  • SEARCH
  • Home
  • 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 8197995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:51:29+00:00 2026-06-07T05:51:29+00:00

ok so im working on my program which will play a song once a

  • 0

ok so im working on my program which will play a song once a button is clicked, i have the code down, and my audio is playing when the button is clicked,but the only problem now is that my program is going completely bonkers and just playing the audio when the button is clicked!!!! the program just freezes the audio keeps playing but the program will not do it’s other functions! What am i doing wrong? and how do i fix it?

heres the code for the action listeners button

in myFrame.java

sound sound = new sound();
private File pokemonBattle = new File(".\\audio\\"+"sound.wav");


private void dualButtonActionPerformed(java.awt.event.ActionEvent evt)       
    {                                           
    // TODO add your handling code here:
    for (int i = 0; i<1; i++){
    c = deck.getNextCard();
    p1hand[i] = c;
    p1.setCard(c);                   //ignore all this
    c = deck.getNextCard();
    p2hand[i] = c;
    p2.setCard(c);
     }

    pokemon1.setEnabled(true);
    pokemon2.setEnabled(true);
    pokemon1.setIcon(p1hand[0].getImage()); //ignore all this as well
    pokemon2.setIcon(p2hand[0].getImage());
    textArea.setText("Pokemon 1:"+p1hand[0].toString()+"\nPokemon 2:   
 "+p2hand[0].toString());
    p1ResultLabel.setText("");
    p2ResultLabel.setText("");

      //this is where im calling my audio
     sound.playAudio(pokemonBattle);//this is where im calling my play audio method

 }

sound.java where i have playAudio()

import java.io.File;
import javax.sound.sampled.*;



public class sound {

public void playAudio(File sf){
    AudioFormat audioFormat;
    AudioInputStream audioInputStream;
    SourceDataLine sourceDataLine;

    try
    {
        audioInputStream = AudioSystem.getAudioInputStream(sf);
        audioFormat = audioInputStream.getFormat();
        System.out.println(audioFormat);

        DataLine.Info dataLineInfo = new  
  DataLine.Info(SourceDataLine.class,audioFormat);

        sourceDataLine =(SourceDataLine)AudioSystem.getLine(dataLineInfo);

        byte tempBuffer[]=new byte[100000];
        int cnt;
        sourceDataLine.open(audioFormat);
        sourceDataLine.start();
        while((cnt=audioInputStream.read
                     (tempBuffer,0,tempBuffer.length))!=-1){
            if(cnt>0){
                sourceDataLine.write(tempBuffer,0,cnt);
            }

        }

    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.exit(0);
        }
    }

}
  • 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-07T05:51:30+00:00Added an answer on June 7, 2026 at 5:51 am

    You need to play the audio in a separate thread. The program becomes unresponsive because it is playing your audio on the event dispatch thread instead of drawing the UI and responding to user interaction.

    instead of:

    sound.playAudio(pokemonBattle);
    

    do:

    Thread t = new Thread( new SoundPlayer( sound, pokemonBattle );
    t.start();
    

    and also:

    public class SoundPlayer implements Runnable
    {
        private final sound sound;
        private final File soundFile;
    
        public SoundPlayer( sound sound, File soundFile )
        {
            this.sound = sound;
            this.soundFile = soundFile;
        }
    
        public void run()
        {
           sound.playAudio( soundFile );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program that I am currently working on which is working
I have a simple Delphi program that I'm working on, in which I am
I'm working on a program which will generate some temporary files, wait for the
I have a C# program which will have multiple instances that need to communicate
I am working on a Delphi program which will display an icon on the
I have written a program which will serialize and de-serialize, it does this fine
I am working on a program which consists of two parts Service component will
I am working on a Java Desktop program which upon its installation will designate
Working in Linux and using C++. I have a program which initiates a thread.
I have this following snippet of code which will when I connect to work,

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.