我的目标是播放短语"这是一个测试",这是一个WAV音频文件。到目前为止,我成功地生成了一个短语的随机序列,例如"测试就是这个",但我需要做到这样它只能成对说话,所以"这是"这是" ; ..."是" ...每个字之间有100 m.second的暂停,每对之间有400 m.second的暂停。这就是我到目前为止......
public void playDoublets(int numDoublets) throws InterruptedException {
ArrayList<Sound> list = new ArrayList();
for (int i =0; i < numWords; i++){
list.add(new Sound(myWordArray[i]));
}
Collections.shuffle(list);
for (int i = 0; i < numWords; i++){
list.get(i).blockingPlay();
Thread.sleep(400);
}
}
numDoublets是程序播放的对数,但我不知道在上面的代码中我将在何处实现它。到目前为止所有这些代码只打印出所有4个单词,但不是成对出现。
答案 0 :(得分:0)
您应该在要播放的声音列表中明确表示暂停。使用界面一般处理正常声音和暂停。
import java.util.*;
public class DoubletPlayer {
private static final int PAUSE_BETWEEN_WORDS = 100;
private static final int PAUSE_BETWEEN_DOUBLETS = 400;
public static void main(String... args) {
DoubletPlayer player = new DoubletPlayer();
player.playDoublets(2);
}
interface Playable {
void play();
}
class Sound implements Playable {
private String word;
public Sound(String word) {
this.word = word;
}
@Override
public void play() {
System.out.println("sound played: " + word);
}
}
class Pause implements Playable {
private int millis;
public Pause(int millis) {
this.millis = millis;
}
@Override
public void play() {
System.out.println("pause of " + millis + "ms");
}
}
private List<Sound> sounds = new ArrayList<>();
private int currentSound = 0;
public DoubletPlayer() {
sounds.add(new Sound("This"));
sounds.add(new Sound("is"));
sounds.add(new Sound("a"));
sounds.add(new Sound("test"));
Collections.shuffle(sounds);
}
public void playDoublets(int numDoublets) {
List<Playable> doublets = generateDoublets(numDoublets);
playAll(doublets);
reset();
}
private List<Playable> generateDoublets(int numDoublets) {
List<Playable> result = new ArrayList<>();
for (int i = 0; i < numDoublets; i++) {
addDoubletTo(result);
}
return result;
}
private void addDoubletTo(List<Playable> result) {
result.add(getNextSound());
result.add(new Pause(PAUSE_BETWEEN_WORDS));
result.add(getNextSound());
result.add(new Pause(PAUSE_BETWEEN_DOUBLETS));
}
private Playable getNextSound() {
return sounds.get(currentSound++);
}
private void playAll(List<Playable> result) {
for (Playable playable : result) {
playable.play();
}
}
private void reset() {
currentSound = 0;
}
}