有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java播放短(小于0:00:500秒)声音片段

我遇到了用java播放短声音片段的问题。有什么建议吗

public static void playAll(String note, String beat, String chord) {

    try {
        AudioInputStream audioInputSoloNote = AudioSystem.getAudioInputStream(new File(
                "C:/java_support/solo_sound/"+note+"_"+beat+".wav").getAbsoluteFile());
        AudioFormat formatNote = audioInputSoloNote.getFormat();

        AudioInputStream audioInputSoloChord = AudioSystem.getAudioInputStream(new File(
                "C:/java_support/solo_chord/"+chord+".wav").getAbsoluteFile());

        Clip clipSolo = AudioSystem.getClip();
        clipSolo.open(audioInputSoloNote);
        clipSolo.start();

        Clip clipChord = AudioSystem.getClip();
        clipChord.open(audioInputSoloChord);
        clipChord.start();

        long frames = audioInputSoloNote.getFrameLength();
        double durationInSeconds = ((frames + 0.0) / formatNote.getFrameRate());

        sleep((long) durationInSeconds * 1000);

        clipSolo.stop();
        clipChord.stop();


    } catch(Exception ex) {
        System.out.println("Error with playing sound.");
        ex.printStackTrace();
    }
}

问题:我正在写一份申请书,用于研究古典音乐创作中的“人工智能”实现。有三种不同的音符(从持续时间的角度来看)——半音符(1.2秒)、四分音符(0.6秒)和八分音符(0.3秒)。这段代码以正确的方式播放Half和Querter音符,然而,当涉及到第八个音符时,它只是跳过它们

即:

C Quarter C --> play (0.6sec duration) 
F Eighth F --> skip (0.3sec)
E Eighth C --> skip (0.3sec)
F Quarter F --> play (0.6sec)
F Quarter Dm --> play (0.6sec)
C Half F --> play (1.2sec)

... ... ...

解决方案可能会增加持续时间秒,但是,它会扩展所有声音,我不想这样做

此外,我还想了解安卓系统的适应性: i、 我想按顺序播放7个短音

private void playNotes (ArrayList<Data> list) {

    SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

    for (int i = 0; i < list.size(); i++) {

        String note = list.get(i).getNote().toLowerCase()+"_eighth";

        int resID = getResources().getIdentifier(note, "raw",

    getPackageName());

        int soundId = sp.load(this, resID, 1);
        sp.play(soundId);
        MediaPlayer mPlayer = MediaPlayer.create(this, resID);

        mPlayer.start();

    }
}

我得到的是所有的声音片段都在同一时间播放。如何修复它


共 (1) 个答案

  1. # 1 楼答案

    我们已经找到了解决办法。我不知道这是不是坏代码,但是,我的项目工程完美

        try
                {
                    Thread.sleep(1000);
                }
                catch(InterruptedException ex)
                {
                    Thread.currentThread().interrupt();
                }