播放声音代表数字

时间:2015-05-12 21:15:15

标签: java swing audio timer javasound

我试图播放代表生成数字的声音时出现问题。

我能够播放第一个号码但由于某种原因,以下号码没有声音出现。

目前我有一个按钮,用于测试设置声音的每个个体号码。

代码如下:已编辑

String s = tTo.getText();
            String t = tFrom.getText();
            try {
                int x = Integer.parseInt(t);
                int d = Integer.parseInt(s);
                if (x >= d) {
                    JOptionPane
                            .showMessageDialog(f,
                                    "START value is higher or Equal to END value, please enter a lower number...");
                }
                rand1 = (int) (Math.random() * (d + 1 - x) + x);
                // rn1
                if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 0) {
                    rn1 = 0;
                    index4 = 1;
                    call1 = "C:/FortuneApps/Randomiser/0.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 1) {
                    rn1 = 1;
                    index4 = 6;
                    call1 = "C:/FortuneApps/Randomiser/1.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 2) {
                    rn1 = 2;
                    index4 = 11;
                    call1 = "C:/FortuneApps/Randomiser/2.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 3) {
                    rn1 = 3;
                    index4 = 16;
                    call1 = "C:/FortuneApps/Randomiser/3.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 4) {
                    rn1 = 4;
                    index4 = 21;
                    call1 = "C:/FortuneApps/Randomiser/4.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 5) {
                    rn1 = 5;
                    index4 = 26;
                    call1 = "C:/FortuneApps/Randomiser/5.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 6) {
                    rn1 = 6;
                    index4 = 31;
                    call1 = "C:/FortuneApps/Randomiser/6.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 7) {
                    rn1 = 7;
                    index4 = 36;
                    call1 = "C:/FortuneApps/Randomiser/7.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 8) {
                    rn1 = 8;
                    index4 = 41;
                    call1 = "C:/FortuneApps/Randomiser/8.wav";
                } else if (rand1 / 10 / 10 / 10 / 10 / 10 % 10 == 9) {
                    rn1 = 9;
                    index4 = 46;
                    call1 = "C:/FortuneApps/Randomiser/9.wav";
                }

等等每个随机数。

所以一切正常,因为它每次都获得第一个数字。并显示一张代表该号码的图片。

我还有一个播放声音的计时器:

代码如下:已编辑

timer8 = new Timer(20, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            index9++;
if (index9 == 50) {
                timer8.setDelay(20);
                if (rand1 >= 100000) {
                    try {
                        as1 = AudioSystem.getAudioInputStream(new File(
                                call1));
                        c1 = AudioSystem.getClip();
                        c1.open(as1);
                    } catch (LineUnavailableException | IOException
                            | UnsupportedAudioFileException e1) {
                        JOptionPane
                                .showMessageDialog(f, "Sound has Failed");
                    }
                    c1.start();
                } else if (rand1 >= 10000) {
                    try {
                        as2 = AudioSystem.getAudioInputStream(new File(
                                call2));
                        c2 = AudioSystem.getClip();
                        c2.open(as2);
                    } catch (LineUnavailableException | IOException
                            | UnsupportedAudioFileException e1) {
                        JOptionPane
                                .showMessageDialog(f, "Sound has Failed");
                    }
                    c2.start();
                } else if (rand1 >= 1000) {
                    try {
                        as3 = AudioSystem.getAudioInputStream(new File(
                                call3));
                        c3 = AudioSystem.getClip();
                        c3.open(as3);
                    } catch (LineUnavailableException | IOException
                            | UnsupportedAudioFileException e1) {
                        JOptionPane
                                .showMessageDialog(f, "Sound has Failed");
                    }
                    c3.start();
                } else if (rand1 >= 100) {
                    try {
                        as4 = AudioSystem.getAudioInputStream(new File(
                                call4));
                        c4 = AudioSystem.getClip();
                        c4.open(as4);
                    } catch (LineUnavailableException | IOException
                            | UnsupportedAudioFileException e1) {
                        JOptionPane
                                .showMessageDialog(f, "Sound has Failed");
                    }
                    c4.start();
                } else if (rand1 < 100) {
                    try {
                        as5 = AudioSystem.getAudioInputStream(new File(
                                call5));
                        c5 = AudioSystem.getClip();
                        c5.open(as5);
                    } catch (LineUnavailableException | IOException
                            | UnsupportedAudioFileException e1) {
                        JOptionPane
                                .showMessageDialog(f, "Sound has Failed");
                    }
                    c5.start();
                } else if (index9 == 100) {
                    if (rand1 / 10 >= 10000) {
                        try {
                            as2 = AudioSystem.getAudioInputStream(new File(
                                    call2));
                            c2 = AudioSystem.getClip();
                            c2.open(as2);
                        } catch (LineUnavailableException | IOException
                                | UnsupportedAudioFileException e1) {
                            JOptionPane.showMessageDialog(f,
                                    "Sound has Failed");
                        }
                        c2.setFramePosition(0);
                        c2.start();
                    } else if (rand1 / 10 >= 1000) {
                        try {
                            as3 = AudioSystem.getAudioInputStream(new File(
                                    call3));
                            c3 = AudioSystem.getClip();
                            c3.open(as3);
                        } catch (LineUnavailableException | IOException
                                | UnsupportedAudioFileException e1) {
                            JOptionPane.showMessageDialog(f,
                                    "Sound has Failed");
                        }
                        c3.setFramePosition(0);
                        c3.start();
                    } else if (rand1 / 10 >= 100) {
                        try {
                            as4 = AudioSystem.getAudioInputStream(new File(
                                    call4));
                            c4 = AudioSystem.getClip();
                            c4.open(as4);
                        } catch (LineUnavailableException | IOException
                                | UnsupportedAudioFileException e1) {
                            JOptionPane.showMessageDialog(f,
                                    "Sound has Failed");
                        }
                        c4.setFramePosition(0);
                        c4.start();
                    } else if (rand1 / 10 < 100) {
                        try {
                            as5 = AudioSystem.getAudioInputStream(new File(
                                    call5));
                            c5 = AudioSystem.getClip();
                            c5.open(as5);
                        } catch (LineUnavailableException | IOException
                                | UnsupportedAudioFileException e1) {
                            JOptionPane.showMessageDialog(f,
                                    "Sound has Failed");
                        }
                    }
                    c5.setFramePosition(0);
                    c5.start();
                }
 //and so on for each digit
                    timer8.stop();
                }

            }
        }
    });

    timer8.setInitialDelay(5);
}

所以在代码调用的第一部分是一个字符串,它在我的电脑上找到音频文件。 我不知道它是如何只播放第一个号码而不是其他号码。 起初我以为声音可能会合在一起,所以我设置了更长的延迟,但这并没有改变任何东西。

EDITED

timer8在我的timer7结束时启动(timer7显示代表数字的最后一个图像)

这里是我试图做的事情,一旦数字被显示,剪辑将播放说祝贺号码:1,2,3

目前所有得到的是祝贺号码:1(以下号码不起作用)

我先尝过其他数字0到9,所有数字都作为第一个数字。

我已将数字的大小从6位改为2位,但仍然只播放第一个数字。

任何帮助都会很棒。

0 个答案:

没有答案