Hallo,
ich schreibe aktuell eine kleines Spiel mit Java FX. In diesem Spiel soll es die Funktion Sound aufnehmen geben, welche durch den Button btn_create_sound aufgerufen wird. Abgebrochen und gespeichert soll das Ganze über den btn_stop_sound. Dazu habe ich folgenden Code geschrieben. Ich bekomme es allerdings nur hin, dass der Thread ein paar Sekundden läuft in dem ich die Zeile Thread.sleep(ZEIT) eingebe und die Varaible Zeit mit einem bestimmten Wert belege.
Ich möchte das ganze aber mit dem Buttonklick beenden und nach einer bestimmten Zeit.
Anbei der Code:
[CODE lang="java" title="Das ist die Controller Klasse"]
public void btn_create_sound(ActionEvent event) throws IOException {
System.out.println("create sound file");
System.out.println(JavaSoundRecorder.wait);
JavaSoundRecorder.startRecorder();
}
@FXML Button stop_recording;
public void btn_stop_record(ActionEvent e) {
JavaSoundRecorder.wait = false;
notify(); // soll wait sagen das der Thread aufwachen kann
}[/CODE]
[CODE lang="java" title="Das ist der Code der Aufnahme Klasse" highlight="96-100"]package application;
import javax.sound.sampled.*;
import java.io.*;
/**
* A sample program is to demonstrate how to record sound in Java
* author: www.codejava.net
*/
public class JavaSoundRecorder {
// record duration, in milliseconds
static final long RECORD_TIME = 30000; //30sekunden
File wavfile = null;
// format of audio file
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
volatile static boolean wait = true;
// the line from which audio data is captured
TargetDataLine line;
/**
* Defines an audio format
*/
AudioFormat getAudioFormat() {
float sampleRate = 41000.0F;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
channels, signed, bigEndian);
return format;
}
/**
* Captures the sound and record into a WAV file
*/
void start() {
try {
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start(); // start capturing
System.out.println("Start capturing...");
AudioInputStream ais = new AudioInputStream(line);
// start recording
System.out.println("Start recording...");
for(int i = 0; i < 300; i++) {
wavfile = new File("src/main/java/application/sounds/ownaudio"+i+".wav");
break;
}
AudioSystem.write(ais, fileType, wavfile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Closes the target data line to finish capturing and recording
*/
void finish() {
line.stop();
line.close();
System.out.println("Finished");
}
/**
* Entry to run the program
*/
public static void startRecorder() {
final JavaSoundRecorder recorder = new JavaSoundRecorder();
// creates a new thread that waits for a specified
// of time before stopping
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
wait(); // soll stoppen bis notify kommt
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.finish();
}
});
stopper.start();
// start recording
recorder.start();
}
}[/CODE]
An der hervorgebonene Stelle hatte ich folgendes ausprobiert:
ich schreibe aktuell eine kleines Spiel mit Java FX. In diesem Spiel soll es die Funktion Sound aufnehmen geben, welche durch den Button btn_create_sound aufgerufen wird. Abgebrochen und gespeichert soll das Ganze über den btn_stop_sound. Dazu habe ich folgenden Code geschrieben. Ich bekomme es allerdings nur hin, dass der Thread ein paar Sekundden läuft in dem ich die Zeile Thread.sleep(ZEIT) eingebe und die Varaible Zeit mit einem bestimmten Wert belege.
Ich möchte das ganze aber mit dem Buttonklick beenden und nach einer bestimmten Zeit.
Anbei der Code:
[CODE lang="java" title="Das ist die Controller Klasse"]
public void btn_create_sound(ActionEvent event) throws IOException {
System.out.println("create sound file");
System.out.println(JavaSoundRecorder.wait);
JavaSoundRecorder.startRecorder();
}
@FXML Button stop_recording;
public void btn_stop_record(ActionEvent e) {
JavaSoundRecorder.wait = false;
notify(); // soll wait sagen das der Thread aufwachen kann
}[/CODE]
[CODE lang="java" title="Das ist der Code der Aufnahme Klasse" highlight="96-100"]package application;
import javax.sound.sampled.*;
import java.io.*;
/**
* A sample program is to demonstrate how to record sound in Java
* author: www.codejava.net
*/
public class JavaSoundRecorder {
// record duration, in milliseconds
static final long RECORD_TIME = 30000; //30sekunden
File wavfile = null;
// format of audio file
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
volatile static boolean wait = true;
// the line from which audio data is captured
TargetDataLine line;
/**
* Defines an audio format
*/
AudioFormat getAudioFormat() {
float sampleRate = 41000.0F;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
channels, signed, bigEndian);
return format;
}
/**
* Captures the sound and record into a WAV file
*/
void start() {
try {
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start(); // start capturing
System.out.println("Start capturing...");
AudioInputStream ais = new AudioInputStream(line);
// start recording
System.out.println("Start recording...");
for(int i = 0; i < 300; i++) {
wavfile = new File("src/main/java/application/sounds/ownaudio"+i+".wav");
break;
}
AudioSystem.write(ais, fileType, wavfile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Closes the target data line to finish capturing and recording
*/
void finish() {
line.stop();
line.close();
System.out.println("Finished");
}
/**
* Entry to run the program
*/
public static void startRecorder() {
final JavaSoundRecorder recorder = new JavaSoundRecorder();
// creates a new thread that waits for a specified
// of time before stopping
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
wait(); // soll stoppen bis notify kommt
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.finish();
}
});
stopper.start();
// start recording
recorder.start();
}
}[/CODE]
An der hervorgebonene Stelle hatte ich folgendes ausprobiert:
Java:
while(wait == true){
try {
Thread.sleep();
}
catch{}
}