Пример:
For(int i=0; i<4; i++)
playSound("Sound.wav");
У меня есть следующие классы:
Основной
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);
}
}
кодировщик
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);
}
}
Задача
Программа воспроизводит звук только один раз. Я подозреваю, что есть проблема блокировки потоков.
Желаемые результаты:
Введите фразу: Hello
аудио воспроизводится так, как вы ожидаете от азбуки Морзе