Ich zeichne eine Audioaufnahme auf.
Dannach spiele ich eine gespeicherte Aufnahme ab und im Anschluss die aufgenommene Audiodatei.
Die aufgenommene Datei wird nur zum Teil abgespielt, auch wird setOnEndOfMedia nicht ausgelöst. Die erste wird immer komplett abgespielt. Ich habe auch schon die Reihenfolge gewechselt dann stock es bei der ersten Datei. Also liegt das Problem bei der aufgenommenen Datei.
Nach mehrmaligen Abspielen wird dann immer mehr der Datei abgespielt bis irgendwann mal das Ende erreicht wird und auch setOnEndOfMedia erreicht wird.
Ich habe bereits getestet ob der Thread der die Aufnahme speichert noch läuft, das ist nicht der Fall. Wenn ich die Datei mit einem anderen Player über das Betriebssystem abspiele läuft diese komplett ab.
Zeit spielt auch keine Rolle, man muss mehrfach abspielen um die Datei komplett zu hören.
Was mach ich falsch, oder kennt jemand den Effekt.
Hier mein Code:
Hier die Aufnahmeklasse :
Dannach spiele ich eine gespeicherte Aufnahme ab und im Anschluss die aufgenommene Audiodatei.
Die aufgenommene Datei wird nur zum Teil abgespielt, auch wird setOnEndOfMedia nicht ausgelöst. Die erste wird immer komplett abgespielt. Ich habe auch schon die Reihenfolge gewechselt dann stock es bei der ersten Datei. Also liegt das Problem bei der aufgenommenen Datei.
Nach mehrmaligen Abspielen wird dann immer mehr der Datei abgespielt bis irgendwann mal das Ende erreicht wird und auch setOnEndOfMedia erreicht wird.
Ich habe bereits getestet ob der Thread der die Aufnahme speichert noch läuft, das ist nicht der Fall. Wenn ich die Datei mit einem anderen Player über das Betriebssystem abspiele läuft diese komplett ab.
Zeit spielt auch keine Rolle, man muss mehrfach abspielen um die Datei komplett zu hören.
Was mach ich falsch, oder kennt jemand den Effekt.
Hier mein Code:
Code:
public void handletrainyourvoicedone () throws InterruptedException {
Media teachervoice = new Media(new File(workingDir + fs + vokidtest + ".mp3").toURI().toString());
Media yourvoice = new Media(new File(workingDir + fs + "U" + userid + ".wav").toURI().toString());
Media[] medialst = new Media[2];
medialst[0] = teachervoice;
medialst[1] = yourvoice;
SequentialMedia sqmed = new SequentialMedia(medialst);
sqmed.play();
}
private class SequentialMedia {
LocalFileToolClass lftc = new LocalFileToolClass();
TransformClass trfc = new TransformClass();
private final Media[] medias;
private int next;
private MediaPlayer player;
public SequentialMedia(Media... medias) {
this.medias = medias;
}
private void playNext() {
dispose();
System.out.println("next : " + next);
if (next >= medias.length){
System.out.println("mache schluss");
allDone();
return;
}
player = new MediaPlayer(medias[next++]);
player.setOnEndOfMedia(this::playNext);
player.play();
System.out.println("Dauer : " + player.getError());
}
public void play() {
next = 0;
playNext();
}
public void dispose() {
if (player != null)
player.dispose();
}
public void allDone() {
// set points
points = points + 3;
trainyourvoicepoints.setText("Your Points : " + trfc.get_Str_F_Int(points));
// clean labels
trainyourvoicevokbaselanguage.setText("");
trainyourvoicelabelword.setText("");
// delete voicefiles in tyv
lftc.DelFile(workingDir + fs + "U" + userid + ".wav");
//lftc.DelFile(workingDir + fs + vokidtest + ".wav");
lftc.DelFile(workingDir + fs + vokidtest + ".mp3");
}
}
Hier die Aufnahmeklasse :
Code:
public class RecordingVoiceFile {
final long recordTime;
final String voicefilename;
final String pathvoicefile;
final ProgressBar progbar ;
/**
* The sample rate - 44,100 Hz for CD quality audio.
*/
public static final int SAMPLE_RATE = 44100;
private static final int BYTES_PER_SAMPLE = 2; // 16-bit audio
private static final int BITS_PER_SAMPLE = 16; // 16-bit audio
private static final double MAX_16_BIT = Short.MAX_VALUE; // 32,767
private static final int SAMPLE_BUFFER_SIZE = 4096;
private static byte[] buffer; // our internal buffer
public TargetDataLine targetLine = null;
public RecordingVoiceFile(long lngrecordTime, String strpathvoicefile, String strvoicefilename, ProgressBar probar) throws LineUnavailableException
{
this.recordTime = lngrecordTime;
this.pathvoicefile = strpathvoicefile;
this.voicefilename = strvoicefilename;
this.progbar = probar;
System.out.println("AUDIO RECORDING START:");
progbar.setProgress(-1);
AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
//AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// Prüft ob das LocalSystem diese Audios erzeugen kann
if(AudioSystem.isLineSupported(info)){
System.err.println("Line supported");
targetLine = (TargetDataLine)AudioSystem.getLine(info);
// öffnet den kanal um zu lesen
targetLine.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
System.out.println("Start Recording: ");
// the internal buffer is a fraction of the actual buffer size, this choice is arbitrary
// it gets divided because we can't expect the buffered data to line up exactly with when
// the sound card decides to push out its samples.
buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3];
// ließt die audio infos schreibt sie aber noch nicht in eine datei
targetLine.start();
Thread thread = new Thread(){
@Override public void run() {
AudioInputStream audioStream = new AudioInputStream(targetLine);
File audioFile = new File(pathvoicefile, voicefilename);
try {
AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, audioFile);
} catch (IOException io) {
io.printStackTrace();
}
}
};
thread.start();
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(recordTime),ae -> { stopRecording(); }));
timeline.play();
} else {
System.err.println("Audio not supported from system");
progbar.setProgress(0);
}
}
private void stopRecording() {
System.out.println("STOP");
targetLine.stop();
targetLine.drain();
targetLine.close();
progbar.setProgress(0);
}
}