dimanche 29 octobre 2017

choose a file randomly in java

Hi I've been studying java for three weeks and doing a tutorial, where I am trying to emit two kinds of sound randomly using 'Clip' and 'File'. I am thinking of choosing a file randomly but I don't know how. I succeeded to emit sounds by using PlaySound method but can't go further.

Here is my code, which emits only one sound Kurzweil1.

import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Tutorial {

public static void main(String[] args) {
    // TODO Auto-generated method stub
File Kurzweil1 = new File("C:\\Users\\ponnp\\Downloads\\Kurzweil-K2000-Dual-Bass-C1.wav");
File Kurzweil2 = new File("C:\\Users\\ponnp\\Downloads\\Kurzweil-K2000-Grand-Strings-C3.wav");

PlaySound(Kurzweil1);           
}

static void PlaySound(File Sound) 
{
    try {
        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(Sound));
        clip.start();

        Thread.sleep(clip.getMicrosecondLength()/1000);
    }catch(Exception e)
    {           

    }
}

}




Aucun commentaire:

Enregistrer un commentaire