簡體   English   中英

播放短(小於0:00:500秒)的聲音片段

[英]Play short (less than 0:00:500 sec) sound clip

我在用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();
    }
}

問題:我正在為古典音樂創作中的“ AI”實現研究編寫一個應用程序。 從持續時間角度來看,有3種不同的音符-一半(1.2秒),四分之一(0.6秒)和八分之一(0.3秒)。 這段代碼以正確的方式演奏Half和Querter音符,但是,當涉及到Halfth音符時,它會跳過它們。

即:

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)

... ... ...

解決方案可能會讓我增加durationInSeconds,但是,它將擴展所有聲音,並且我不想這樣做。

我也想找出適應android的方法:即我想按順序播放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();

    }
}

我得到的是所有聲音片段同時播放。 如何解決?

找到了解決方案。 不知道它是否是損壞的代碼,但是,因為我的項目運行良好

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM