Java: Console stops working when using javazoom - java

When I start playing music by javazoom library, console stops answer. I can write anything, but no response.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;
public class Main {
public static void men(int i) {
try {
FileInputStream fis = new FileInputStream("Filename.WAV");
AdvancedPlayer player = new AdvancedPlayer(fis);
player.play();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(JavaLayerException e) {
e.printStackTrace();
}
}
public static Scanner scn = new Scanner(System.in);
public static void main(String[] args) {
while(true) {
int i = scn.nextInt();
if(i == 1) {
men(i);
}
if(i == 5) {
System.out.println("i am here");
}
}
}
}
Is there solution? Or javazoom can't play audio and do other things?

I'm not clear what you mean by "console stops answer" and "Javazoom can't play audio and do other things."
You can execute the javazoom play command in its own thread. While that thread runs concurrently, your program will be free to execute other tasks.
Do you know how to create and launch a thread? Here is Oracle's tutorial Processes and Threads.

Related

Return to main after thread has ended

I am currently messing around with facial recognition and try to capture photos form my webcam. I am adapting this tutorial to automatically name and save the taken picture. Note that this Code is called from the main() the funtion it self is implemented in another class. Up and until I stop the thread with interrupt() it works. Afterwards the picture is frozen in the GUI and the system doesn't seem to return into the class where I operate over my GUI.
To make myself clear: I want to capture a picture from my webcam and replace the former stream from my webcam with said earlier captured picture. Up and until I capture the picture and interrupt the thread the code works. Aftwards it is stuck.
I experiemented with pulling the Thread into the ActionListener of the CaptureButton but that failed because the main GUI element was not accessible which is courious given that it is defined as public.
It does however throw in the course of compilation some warnings - tho I must admit that I have no clue what they mean:
[ WARN:0] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\videoio\src\cap_msmf.cpp (376) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): OnReadSample() is called with error status: -1072873821
[ WARN:0] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\videoio\src\cap_msmf.cpp (388) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error status: -1072873821
[ WARN:1] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\videoio\src\cap_msmf.cpp (1021) CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -1072873821
Exception in thread "Thread-0" CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.5.2) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\imgcodecs\src\loadsave.cpp:896: error: (-215:Assertion failed) !image.empty() in function 'cv::imencode'
]
at org.opencv.imgcodecs.Imgcodecs.imencode_1(Native Method)
at org.opencv.imgcodecs.Imgcodecs.imencode(Imgcodecs.java:510)
at GUI.FaceRecognition.startCamera(FaceRecognition.java:129)
at main$1$1.run(main.java:19)
at java.base/java.lang.Thread.run(Thread.java:834)
[ WARN:2] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Thread vorbei
I have written my code rather amateurish of that I am sure and I reckon I could use something like callable() but am unsure who to use that. I also do not know how to replicate my problem outside of chucking my entire project around the internet but I can however provide the code of my modifications:
for the funtion itself
public boolean startCamera(){
capture= new VideoCapture(0);
image=new Mat();
byte[] imageData;
ImageIcon icon;
String name = null;
boolean echo = false;
while(true){
capture.read(image);
final MatOfByte buf=new MatOfByte();
Imgcodecs.imencode(".png", image,buf);
imageData= buf.toArray();
icon=new ImageIcon(imageData);
Feed.setIcon(icon);
if(trigger==true){
name="Recognition";
Imgcodecs.imwrite("src/"+name+".png",image);
trigger=false; //Auslöser zurücksetzten
}
File kamera=new File("src/Recognition.png");
if(kamera.exists()==true){
//capture.release();
/*try {
Kamera=ImageIO.read(this.getClass().getResourceAsStream("/Recognition.png"));
} catch (IOException e) {
e.printStackTrace();
}*/
return echo=true;
}
}
}
for the main()
import java.awt.*;
import java.io.File;
import java.lang.String;
import GUI.*;
import org.opencv.core.Core;
public class main {
public static void main(String[] args){
FaceRecognition Fenster=new FaceRecognition();
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
//FaceRecognition Fenster=new FaceRecognition();
new Thread (new Runnable() {
#Override
public void run() {
Fenster.startCamera();
}
}).start();
//File kamera=new File(("src/Recognition.png"));
}
});
if(Fenster.startCamera()==true){
Thread.currentThread().interrupt();
System.out.println("Thread vorbei");
}
}
}
Please point out where I went wrong or what goes wrong.
Edit:
I have mean while heeded #pveentjers advice and reformed my code (see below). Yet in my main() the IDE proposes to make startCamera static which will not work because startCamera can not be static (or rather elements of it can't). Is there any way to move around this?
the startCamera function
public class startCamera implements Runnable{
private CountDownLatch end;
public startCamera(CountDownLatch one){
this.end=one;
}
#Override public void run(){
try {
capture= new VideoCapture(0);
image=new Mat();
byte[] imageData;
ImageIcon icon;
String name = null;
while(true){
capture.read(image);
final MatOfByte buf=new MatOfByte();
Imgcodecs.imencode(".png", image,buf);
imageData= buf.toArray();
icon=new ImageIcon(imageData);
Feed.setIcon(icon);
if(trigger==true){
name="Recognition";
Imgcodecs.imwrite("src/"+name+".png",image);
trigger=false; //Auslöser zurücksetzten
}
File kamera=new File("src/Recognition.png");
if(kamera.exists()==true){
capture.release();
end.countDown();
/*try {
Kamera=ImageIO.read(this.getClass().getResourceAsStream("/Recognition.png"));
} catch (IOException e) {
e.printStackTrace();
}*/
}
}
}
catch (Exception exception){
}
}
the reformed main()
import java.awt.*;
import java.io.File;
import java.lang.String;
import java.util.concurrent.CountDownLatch;
import GUI.*;
import GUI.FaceRecognition.startCamera;
import org.opencv.core.Core;
public class main {
public static void main(String[] args){
FaceRecognition Fenster=new FaceRecognition();
CountDownLatch mark=new CountDownLatch(1);
startCamera startCamera=new startCamera(mark);
try {
mark.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
Might be that I don't the wood for the trees but this the first time I am working with threads.

Runtime.exec() in java hangs because it is waiting for input from System.in

I have the following short python program "test.py"
n = int(raw_input())
print n
I'm executing the above program from following java program "ProcessRunner.java"
import java.util.*;
import java.io.*;
public class ProcessRunner {
public static void main(String[] args) {
try {
Scanner s = new Scanner(Runtime.getRuntime().exec("python test.py").getInputStream()).useDelimiter("\\A");
System.out.println(s.next());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Upon running the command,
java ProcessRunner
I'm not able to pass a value 'n' in proper format to Python program and also the java run hangs. What is the proper way to handle the situation and pass a value to 'n' dynamically to python program from inside java program?
raw_input(), or input() in Python 3, will block waiting for new line terminated input on standard input, however, the Java program is not sending it anything.
Try writing to the Python subprocess using the stream returned by getOutputStream(). Here's an example:
import java.util.*;
import java.io.*;
public class ProcessRunner {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("python test.py");
Scanner s = new Scanner(p.getInputStream());
PrintWriter toChild = new PrintWriter(p.getOutputStream());
toChild.println("1234"); // write to child's stdin
toChild.close(); // or you can use toChild.flush()
System.out.println(s.next());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
An alternative is to pass n as a command line argument. This requires modification of the Python script to expect and process the command line arguments, and to the Java code to send the argument:
import java.util.*;
import java.io.*;
public class ProcessRunner {
public static void main(String[] args) {
try {
int n = 1234;
Process p = Runtime.getRuntime().exec("python test.py " + n);
Scanner s = new Scanner(p.getInputStream());
System.out.println(s.next());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
And the Python script, test.py:
import sys
if len(sys.argv) > 1:
print int(sys.argv[1])
If I understand you correctly you want your java program to pass any output from your python script to System.out and any input into your java program to your python Script, right?
Have a look at the following program to get an idea how you could do this.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ProcessRunner {
public static void main(String[] args) {
try {
final Process process = Runtime.getRuntime().exec("/bin/sh");
try (
final InputStream inputStream = process.getInputStream();
final InputStream errorStream = process.getErrorStream();
final OutputStream outputStream = process.getOutputStream()
) {
while (process.isAlive()) {
forwardOneByte(inputStream, System.out);
forwardOneByte(errorStream, System.err);
forwardOneByte(System.in, outputStream);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void forwardOneByte(final InputStream inputStream,
final OutputStream outputStream)
throws IOException {
if(inputStream.available() <= 0) {
return;
}
final int b = inputStream.read();
if(b != -1) {
outputStream.write(b);
outputStream.flush();
}
}
}
Note This code is just a concept demo. It will eat up your cpu and will not be able to cope with bigger amounts of throughput.

playing music continuously in the loop

I have written this code for playing music, this code plays music once but I want to play it continuously in the loop please tell how I can do that?
import java.io.FileInputStream;
import sun.audio.*;
public class A {
public static void main(String arg[]) throws Exception {
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(BGM);
}
}
According to the documentation, start will cause the player to:
Start playing a stream. The stream will continue to play until the stream runs out of data, or it is stopped.
Hence, you could just wrap it in an endless loop:
public static void main(String arg[]) throws Exception {
AudioPlayer MGP = AudioPlayer.player;
while(true) {
AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(BGM);
BGM.close();
}
}
}

Unable to accept filenames as arguments for reading from (or writing to) a file

The following program is meant to read from a file:-
import java.util.*;
import java.io.*;
import java.lang.*;
public class file_read
{
private Scanner input = new Scanner(System.in);
private Scanner fileinput;
public void open() // [1]
{
try
{
String filename = input.next();
fileinput = new Scanner(new File(filename+".txt")); // [2]
}
catch(Exception e)
{
System.out.println("opening error.");
}
}
public void read()
{
// some task to read the file
}
public void closeFile()
{
// closing the file
}
}
Problem lies with [1] and I think that the statement [2] is creating the problem. If I replace the filename+".txt" with the actual filename, everything runs fine. I am unable to pinpoint the reason. Please help.

Exception with Freetts when using kevin or mbrola

I am trying to run a program using freetts. I am able to compile the program however I am not able to use kevin or mbrola voices I get the follwing output message at the end
System property "mbrola.base" is undefined. Will not use MBROLA voices.
LINE UNAVAILABLE: Format is pcm_signed 16000.0 Hz 16 bits 1 channel big endian
import javax.speech.*;
import javax.speech.synthesis.*;
import java.util.*;
class freetts {
public static void main(String[] args) {
try{
Calendar calendar = new GregorianCalendar();
String sayTime = "It is " + calendar.get(Calendar.HOUR) + " " + calendar.get(Calendar.MINUTE) + " " + (calendar.get(Calendar.AM_PM)==0 ? "AM":"PM");
Synthesizer synth = Central.createSynthesizer(null);
synth.allocate();
synth.resume();
synth.speakPlainText(sayTime, null);
synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
synth.deallocate();
}
catch(Exception e){
e.printStackTrace();
}
}
}
It seems that "To enable FreeTTS support for MBROLA, merely copy mbrola/mbrola.jar to lib/mbrola.jar. Then, whenever you run any FreeTTS application, specify the "mbrola.base" directory as a system property:
java -Dmbrola.base=/home/jim/mbrola -jar bin/FreeTTSHelloWorld.jar mbrola_us1"
I found this at:
http://freetts.sourceforge.net/mbrola/README.html
http://workorhobby.blogspot.com/2011/02/java-audio-freetts-line-unavailable.html
A big thanks to the author.
A program based on FreeTTS, the free text-to-speech engine for Java, was getting occasional errors
"LINE UNAVAILABLE: Format is ..."
Turns out there is no Java Exception or other mechanism to detect this error that occurs inside the FreeTTS library. All you get is the message on System.out, so there is no good way to react programatically.
Workaround: Configure the FreeTTS audio player to attempt accessing the audio device more than once until it succeeds. In this example, a short delay of 0.1 seconds is used to not miss an opportunity to grab the audio device; we keep trying for 30 seconds:
System.setProperty("com.sun.speech.freetts.audio.AudioPlayer.openFailDelayMs", "100");
System.setProperty("com.sun.speech.freetts.audio.AudioPlayer.totalOpenFailDelayMs", "30000");
If the audio device is permanently used by another program, there is of course no way to get access. Under Linux, this command will display the ID of the process that is currently holding the audio device, so you can then try to get rid of the offending program:
/sbin/fuser /dev/dsp
The second phrase has nothing to do with mbrola, but with a horrendous java linux sound bug that is still not fixed.
Check the third post here:
https://forums.oracle.com/forums/thread.jspa?threadID=2206163
That is happening because freetts "trusts" the sourcedataline, instead of doing the workaround on that post. The bug is in the jdk, but can be worked around by finding where in freetts that is happening and inserting the workaround & recompiling.
Here is a testcase
package util.speech;
import java.util.Iterator;
import java.util.Locale;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class VoiceTest {
public VoiceTest() {
}
#BeforeClass
public static void setUpClass() throws Exception {
}
#AfterClass
public static void tearDownClass() throws Exception {
}
#Before
public void setUp() {
}
#After
public void tearDown() {
}
#Test
public void testDataLineAvailableAndBuggyInJDK() throws LineUnavailableException {
boolean failedSimpleGetLine = false;
AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
line = (SourceDataLine) AudioSystem.getLine(info);
} catch (LineUnavailableException e) {
//ok, at least it says so
throw e;
}
try {
//if this fails the jdk is very buggy, since it just told us
//the line was available
line.open(format);
} catch (LineUnavailableException e) {
failedSimpleGetLine = true;
} finally {
if (line.isOpen()) {
line.close();
}
}
//now if this is true, test if it's possible to get a valid sourcedataline
//or the only bug is adquiring a sourcedataline doesn't throw a lineunavailable
//exception before open
Assume.assumeTrue(failedSimpleGetLine);
line = getSourceDataLine(format);
if (line == null) {
return;
}
try {
line.open(format);
} catch (LineUnavailableException e) {
//ok then it is consistent, and there is only one bug
fail("Line Unavailable after being adquired");
} finally {
if (line.isOpen()) {
line.close();
}
}
fail("line available after first test not managing to adquire it");
}
private SourceDataLine getSourceDataLine(AudioFormat format) {
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
for (Mixer.Info mi : AudioSystem.getMixerInfo()) {
SourceDataLine dataline = null;
try {
Mixer mixer = AudioSystem.getMixer(mi);
dataline = (SourceDataLine) mixer.getLine(info);
dataline.open(format);
dataline.start();
return dataline;
} catch (Exception e) {
}
if (dataline != null) {
try {
dataline.close();
} catch (Exception e) {
}
}
}
} catch (Exception e) {
}
return null;
}
}
I know i am posting it little late, but this may help someone. I tried with both kevin and mbrola, and it worked for me. Please find the code below.
package com.mani.texttospeech;
import java.beans.PropertyVetoException;
import java.util.Locale;
import javax.speech.AudioException;
import javax.speech.Central;
import javax.speech.EngineException;
import javax.speech.EngineStateError;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Voice;
/**
*
* #author Manindar
*/
public class SpeechUtils {
SynthesizerModeDesc desc;
Synthesizer synthesizer;
Voice voice;
public void init(String voiceName) throws EngineException, AudioException, EngineStateError, PropertyVetoException {
if (desc == null) {
System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
desc = new SynthesizerModeDesc(Locale.US);
Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
synthesizer = Central.createSynthesizer(desc);
synthesizer.allocate();
synthesizer.resume();
SynthesizerModeDesc smd = (SynthesizerModeDesc) synthesizer.getEngineModeDesc();
Voice[] voices = smd.getVoices();
for (Voice voice1 : voices) {
if (voice1.getName().equals(voiceName)) {
voice = voice1;
break;
}
}
synthesizer.getSynthesizerProperties().setVoice(voice);
}
}
public void terminate() throws EngineException, EngineStateError {
synthesizer.deallocate();
}
public void doSpeak(String speakText) throws EngineException, AudioException, IllegalArgumentException, InterruptedException {
synthesizer.speakPlainText(speakText, null);
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
}
public static void main(String[] args) throws Exception {
SpeechUtils su = new SpeechUtils();
su.init("kevin16");
// su.init("kevin");
// su.init("mbrola_us1");
// su.init("mbrola_us2");
// su.init("mbrola_us3");
// high quality
su.doSpeak("Hi this is Manindar. Welcome to audio world.");
su.terminate();
}
}
And add the below dependencies to your pom.xml file.
<dependencies>
<dependency>
<groupId>net.sf.sociaal</groupId>
<artifactId>freetts</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
Hope this will be helpful.

Categories