J'essaie d'obtenir Java jouer un son à un certain taux à cet égard, j'essaie d'obtenir un Controll taux d'échantillonnage pour SourceDataLine:type de contrôle non pris en charge: Sample Rate
`package com.pap.sound;
import javax.sound.sampled.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.BitSet;
public class Player implements Playable {
private AudioFormat format;
private SourceDataLine sourceDataLine;
private DataLine.Info info;
private final URL soundUrl;
private final boolean[] stopped;
private float playRate;
private boolean playRateChanged;
public Player(URL soundUrl) throws LineUnavailableException, MalformedURLException {
this.format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100, 16, 2 , 4, 44100,false);
this.info = new DataLine.Info(SourceDataLine.class, format);
this.sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
this.soundUrl = soundUrl;
this.stopped = new boolean[1];
this.stopped[0] = false;
this.playRate = 1;
this.playRateChanged = false;
}
@Override
public boolean play() throws LineUnavailableException {
sourceDataLine.open();
Thread playerThread = new Thread(){
@Override
public void run() {
int numberOfBitesRead = 0;
AudioInputStream auis = null;
try {
auis = AudioSystem.getAudioInputStream(soundUrl);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = new byte[4];
sourceDataLine.start();
try {
auis.mark(auis.available());
while(!stopped[0]) {
numberOfBitesRead = auis.read(bytes);
if(numberOfBitesRead == -1) {
auis.reset();
}
sourceDataLine.write(bytes, 0, bytes.length);
FloatControl fc = (FloatControl) sourceDataLine.getControl(FloatControl.Type.SAMPLE_RATE);
if(playRateChanged) {
fc.setValue((int)(44200*playRate));
playRateChanged = false;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
playerThread.start();
return false;
}
@Override
public boolean stop() {
this.stopped[0] = true;
this.sourceDataLine.stop();
this.sourceDataLine.close();
return false;
}
@Override
public boolean setPlayRate(float playRate) {
this.playRate = playRate;
this.playRateChanged = true;
return false;
}
} `
Mais à l'exécution je reçois:
Exception in thread "Thread-1" java.lang.IllegalArgumentException: Unsupported control type: Sample Rate
at com.sun.media.sound.AbstractLine.getControl(AbstractLine.java:150)
at com.pap.sound.Player$1.run(Player.java:67)
Avez-vous déjà rencontré quelque chose comme ça? J'ai essayé de trouver une réponse pour cela mais je n'ai pas pu en trouver un. J'utilise Java 8. Nous vous remercions de votre aide.
ce qui est sourceDataLine et FloatControl? partager le code pour cela ... Créer [MCVE] (http://stackoverflow.com/help/mcve) – StackFlowed
'this.format = new AudioFormat (AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, faux); this.info = new DataLine.Info (SourceDataLine.class, format); this.sourceDataLine = (SourceDataLine) AudioSystem.getLine (info); ' –
@StackFlowed Je suis presque certain qu'ils sont [' SourceDataLine'] (https://docs.oracle.com/javase/8/docs/api/javax/sound/sampled/SourceDataLine.html) et ['FloatControl'] (https://docs.oracle.com/javase/8/docs/api/ javax/sound/sampled/FloatControl.html) de ['javax.sound.sampled'] (https://docs.oracle.com/javase/8/docs/api/javax/sound/sampled/package-summary.html). – TNT