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 7753649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:06:20+00:00 2026-06-01T12:06:20+00:00

Example: For(int i=0; i<4; i++) playSound(Sound.wav); I have the following classes: Main import java.io.IOException;

  • 0

Example:

For(int i=0; i<4; i++)
playSound("Sound.wav");

I have the following classes:

Main

import java.io.IOException;
import java.util.*;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class MainClass 
{
    public static void main (String [] args) 
    throws UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException
    {

        Thread main = new Thread();
        Thread sound = new Thread();
        main.start();

        Scanner in = new Scanner (System.in);

        Encode MorseCode = new Encode();


        System.out.print("Enter a Phrase: ");
        String input = in.next();

        String selected = "";
        String converted = "";
        String morse = "";


        for (int i = 0; i < input.length(); i++) 
        {
            //for every character instantiate a new sound object
            playSound ps = new playSound();

            //Select the next character
            selected = input.charAt(i) +"";

            // Convert the character
            converted = MorseCode.getEncode(selected);

            //Trying to  pause the main thread until the sound clip finishes
            if(converted == ".")
            {
                //set main thread to sleep for duration of sound clip
                //main.sleep(ps.getWait()*1000);
                main.yield();

                //start other thread to complete the task of playing the soudn
                sound.start();
                ps.playBlip();
            }   
            main.join();
            morse = morse +" "+converted;
            converted = "";
        }

        System.out.print("Morse Code: "+morse);

    }
}

Encoder

public class Encode
{
    public Encode(){}

    public String getEncode(String toEncode)
    {
        String morse = toEncode;

        if (toEncode.equalsIgnoreCase("a"))
            morse = ".-";
        if (toEncode.equalsIgnoreCase("b"))
            morse = "-...";
        if (toEncode.equalsIgnoreCase("c"))
            morse = "-.-.";
        if (toEncode.equalsIgnoreCase("d"))
            morse = "-..";
        if (toEncode.equalsIgnoreCase("e"))
            morse = ".";
        if (toEncode.equalsIgnoreCase("f"))
            morse = "..-.";
        if (toEncode.equalsIgnoreCase("g"))
            morse = "--.";
        if (toEncode.equalsIgnoreCase("h"))
            morse = "....";
        if (toEncode.equalsIgnoreCase("i"))
            morse = "..";
        if (toEncode.equalsIgnoreCase("j"))
            morse = ".---";
        if (toEncode.equalsIgnoreCase("k"))
            morse = "-.-";
        if (toEncode.equalsIgnoreCase("l"))
            morse = ".-..";
        if (toEncode.equalsIgnoreCase("m"))
            morse = "--";
        if (toEncode.equalsIgnoreCase("n"))
            morse = "-.";
        if (toEncode.equalsIgnoreCase("o"))
            morse = "---";
        if (toEncode.equalsIgnoreCase("p"))
            morse = ".--.";
        if (toEncode.equalsIgnoreCase("q"))
            morse = "--.-";
        if (toEncode.equalsIgnoreCase("r"))
            morse = ".-.";
        if (toEncode.equalsIgnoreCase("s"))
            morse = "...";
        if (toEncode.equalsIgnoreCase("t"))
            morse = "-";
        if (toEncode.equalsIgnoreCase("u"))
            morse = "..-";
        if (toEncode.equalsIgnoreCase("v"))
            morse = "...-";
        if (toEncode.equalsIgnoreCase("w"))
            morse = ".--";
        if (toEncode.equalsIgnoreCase("x"))
            morse = "-..-";
        if (toEncode.equalsIgnoreCase("y"))
            morse = "-.--";
        if (toEncode.equalsIgnoreCase("z"))
            morse = "--..";
        if (toEncode.equalsIgnoreCase("0"))
            morse = "-----";
        if (toEncode.equalsIgnoreCase("1"))
            morse = ".----";
        if (toEncode.equalsIgnoreCase("2"))
            morse = "..---";
        if (toEncode.equalsIgnoreCase("3"))
            morse = "...--";
        if (toEncode.equalsIgnoreCase("4"))
            morse = "....-";
        if (toEncode.equalsIgnoreCase("5"))
            morse = ".....";
        if (toEncode.equalsIgnoreCase("6"))
            morse = "-....";
        if (toEncode.equalsIgnoreCase("7"))
            morse = "--...";
        if (toEncode.equalsIgnoreCase("8"))
            morse = "---..";
        if (toEncode.equalsIgnoreCase("9"))
            morse = "----.";
        if (toEncode.equalsIgnoreCase("."))
            morse = ".-.-";
        if (toEncode.equalsIgnoreCase(","))
            morse = "--..--";
        if (toEncode.equalsIgnoreCase("?"))
            morse = "..--..";

        return morse;
    }
}

PlaySound

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;


public class playSound 
{

    public playSound(){}

    public void playBlip() throws UnsupportedAudioFileException, IOException, LineUnavailableException
    {

        File soundFile = new File("blip.wav");
        AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

        DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(sound);

        clip.addLineListener( new LineListener() 
        {
            public void update(LineEvent event) 
            {
                if (event.getType() == LineEvent.Type.STOP) 
                {
                    event.getLine().close();
                    System.exit(0);
                }
            }
        });

        clip.start();
        clip.drain();
        clip.close();
    }
    public int getSoundDurationForThreadWait() throws UnsupportedAudioFileException, IOException, LineUnavailableException
    {
        File soundFile = new File("blip.wav");
        AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

        DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(sound);

        //making this method return milliseconds since threads waits are in this unit
        return (int) (clip.getMicrosecondLength()/1000);

    }
}        

Problem
Program only plays sound one time. I suspect there is a threading blocking problem.

Desired Results:

Enter phrase: Hello

audio plays as you would expect for a morse code

  • 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-01T12:06:21+00:00Added an answer on June 1, 2026 at 12:06 pm

    The specific problem is that your playBlip() method is designed to terminate the entire program when it reaches the end of the sound clip:

            clip.addLineListener( new LineListener() 
            {
                public void update(LineEvent event) 
                {
                    if (event.getType() == LineEvent.Type.STOP) 
                    {
                        event.getLine().close();
                        System.exit(0); // <------------ terminate the JVM
                    }
                }
            });
    

    But overall, I think you should examine your approach more closely; you have a lot of code that seems to serve no purpose besides obfuscation. For example, you have two Thread instances, main and sound, that are designed to do absolutely nothing: calling main.start() will launch a new thread that exits immediately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a question about the function call in the following example: int main()
I found an example of Objective-C/cocoa framework has the following code. int main() {
If I have the following for example: int *x; x = func(a); For the
Consider the following example: int size = 10, *kk = new int[size]; for (int
I have string value and Type of some variable (For example int and 32
So I have 2 functions that both have similar arguments void example(int a, int
I want to use rb_require with rb_protect, as in the following example: int error;
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,
This is my struct: struct example { int c=0; char buf[10]; } I have
The following question is regarding the speed between selecting an exact match (example: INT)

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.