Méthode Java aléatoire et lecture d'un fichier
fruityshine
-
fruityshine -
fruityshine -
Bonjour,
Je souhaiterais créer une méthode java qui choisisse aléatoirement une chanson comme un nombre de 0 à 2 et qu'ensuite il l'a lise. J'ai ce code pour l'instant
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class tableauqcm{
public static void main(String[] args){
// TODO Auto-generated method stub
{
String qcm[] = {"0", "1", "2", };
int nbAlea = (int) (Math.random()* qcm.length);
System.out.println(qcm[nbAlea]);
{
if ("0"!=null)
{
try{
File fichier = new File("0.wav") ;
AudioInputStream AudioInput = AudioSystem.getAudioInputStream(fichier);
int bytesPerFrame = AudioInput.getFormat().getFrameSize();
int numBytes = 1024 * bytesPerFrame;
byte[] tableau = new byte[numBytes];
AudioFormat audioFormat = AudioInput.getFormat();
DataLine.Info Info = new DataLine.Info(SourceDataLine.class,audioFormat);
SourceDataLine line=(SourceDataLine)AudioSystem.getLine(Info);
line.open(audioFormat);
line.start();
int nb;
while ( (nb = AudioInput.read(tableau,0,numBytes )) != -1 ){
line.write(tableau,0,nb);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
{
if ("1"!=null)
{
try{
File fichier = new File("1.wav") ;
AudioInputStream AudioInput = AudioSystem.getAudioInputStream(fichier);
int bytesPerFrame = AudioInput.getFormat().getFrameSize();
int numBytes = 1024 * bytesPerFrame;
byte[] tableau = new byte[numBytes];
AudioFormat audioFormat = AudioInput.getFormat();
DataLine.Info Info = new DataLine.Info(SourceDataLine.class,audioFormat);
SourceDataLine line=(SourceDataLine)AudioSystem.getLine(Info);
line.open(audioFormat);
line.start();
int nb;
while ( (nb = AudioInput.read(tableau,0,numBytes )) != -1 ){
line.write(tableau,0,nb);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
{
if ("2"!=null)
{
try{
File fichier = new File("2.wav") ;
AudioInputStream AudioInput = AudioSystem.getAudioInputStream(fichier);
int bytesPerFrame = AudioInput.getFormat().getFrameSize();
int numBytes = 1024 * bytesPerFrame;
byte[] tableau = new byte[numBytes];
AudioFormat audioFormat = AudioInput.getFormat();
DataLine.Info Info = new DataLine.Info(SourceDataLine.class,audioFormat);
SourceDataLine line=(SourceDataLine)AudioSystem.getLine(Info);
line.open(audioFormat);
line.start();
int nb;
while ( (nb = AudioInput.read(tableau,0,numBytes )) != -1 ){
line.write(tableau,0,nb);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
}
Le nombre est bien tiré au hasard mais je n'arrive pas à faire lire le bon titre avec le bon numéro. Après selection du nombre c'est toujours le titre 0 qui est lu puis le 1 et le 2.
Comment arriver à lire le bon titre avec le bon nombre ?
Je souhaiterais créer une méthode java qui choisisse aléatoirement une chanson comme un nombre de 0 à 2 et qu'ensuite il l'a lise. J'ai ce code pour l'instant
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class tableauqcm{
public static void main(String[] args){
// TODO Auto-generated method stub
{
String qcm[] = {"0", "1", "2", };
int nbAlea = (int) (Math.random()* qcm.length);
System.out.println(qcm[nbAlea]);
{
if ("0"!=null)
{
try{
File fichier = new File("0.wav") ;
AudioInputStream AudioInput = AudioSystem.getAudioInputStream(fichier);
int bytesPerFrame = AudioInput.getFormat().getFrameSize();
int numBytes = 1024 * bytesPerFrame;
byte[] tableau = new byte[numBytes];
AudioFormat audioFormat = AudioInput.getFormat();
DataLine.Info Info = new DataLine.Info(SourceDataLine.class,audioFormat);
SourceDataLine line=(SourceDataLine)AudioSystem.getLine(Info);
line.open(audioFormat);
line.start();
int nb;
while ( (nb = AudioInput.read(tableau,0,numBytes )) != -1 ){
line.write(tableau,0,nb);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
{
if ("1"!=null)
{
try{
File fichier = new File("1.wav") ;
AudioInputStream AudioInput = AudioSystem.getAudioInputStream(fichier);
int bytesPerFrame = AudioInput.getFormat().getFrameSize();
int numBytes = 1024 * bytesPerFrame;
byte[] tableau = new byte[numBytes];
AudioFormat audioFormat = AudioInput.getFormat();
DataLine.Info Info = new DataLine.Info(SourceDataLine.class,audioFormat);
SourceDataLine line=(SourceDataLine)AudioSystem.getLine(Info);
line.open(audioFormat);
line.start();
int nb;
while ( (nb = AudioInput.read(tableau,0,numBytes )) != -1 ){
line.write(tableau,0,nb);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
{
if ("2"!=null)
{
try{
File fichier = new File("2.wav") ;
AudioInputStream AudioInput = AudioSystem.getAudioInputStream(fichier);
int bytesPerFrame = AudioInput.getFormat().getFrameSize();
int numBytes = 1024 * bytesPerFrame;
byte[] tableau = new byte[numBytes];
AudioFormat audioFormat = AudioInput.getFormat();
DataLine.Info Info = new DataLine.Info(SourceDataLine.class,audioFormat);
SourceDataLine line=(SourceDataLine)AudioSystem.getLine(Info);
line.open(audioFormat);
line.start();
int nb;
while ( (nb = AudioInput.read(tableau,0,numBytes )) != -1 ){
line.write(tableau,0,nb);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
}
Le nombre est bien tiré au hasard mais je n'arrive pas à faire lire le bon titre avec le bon numéro. Après selection du nombre c'est toujours le titre 0 qui est lu puis le 1 et le 2.
Comment arriver à lire le bon titre avec le bon nombre ?
A voir également:
- Méthode Java aléatoire et lecture d'un fichier
- Fichier bin - Guide
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Comment réduire la taille d'un fichier - Guide
- Comment ouvrir un fichier epub ? - Guide
2 réponses
Bonjour
Je ne peux pas t'aider pour la partie Audio, mais pour tirer un nombre aleatoire entre 0 et 2, vous pouvez utiliser:
Quand à l'utilisation de ce nombre aleatoire, " if ("0"!=null) " va toujours retourner true parce que vous comparez une constante avec null, cependant, vous devez tester la valeur de nbAlea avec switch:
Bonne chance
Being normal is boring... being geek is interesting
Je ne peux pas t'aider pour la partie Audio, mais pour tirer un nombre aleatoire entre 0 et 2, vous pouvez utiliser:
int r = new Random().nextInt(3);
Quand à l'utilisation de ce nombre aleatoire, " if ("0"!=null) " va toujours retourner true parce que vous comparez une constante avec null, cependant, vous devez tester la valeur de nbAlea avec switch:
switch(nbAlea) { case 0: // code: affecter l'adresse du fichier a lire à une variable adresse par exemple adresse= "0.wav"; case 1: adresse = "1.wav"; ... }, puis vous pouvez utiliser le meme code de lecture en le regroupant dans une fonction (puisqu'il est le meme pour les trois fichier).
Bonne chance
Being normal is boring... being geek is interesting