I was just wondering if it were possible to link a sound to an option selected in a scanner class in java? For example:

import java.util.*;
public class GameTest
{
    public static void main(String[] args)
    {
        Game myGame = new Game();
        Scanner myScanner = new Scanner(System.in);
        int size = 30;      
        boolean done = false;
        do
        {
            System.out.println("1. Add Person");
            System.out.println("2. Show Board");
            System.out.println("3. Show Next Generation");
            System.out.println("4. Clear Board");
            System.out.println("5. Exit");
            String answer = myScanner.nextLine();
            int option = Integer.parseInt(answer);
            if (option==1)
            {
            Blah, blah, blah
             }

if I select option #1 can I link a path to a sound on my C:????
I know I prob need to import something, and extend something.
Thanks,
James

sure you can and folow how to:

Clip clickClip = AudioSystem.getClip();

/* Create a URL that points to our file */
File f=new File("C:/WINDOWS/Media/CHIMES.wav");
InputStream is=new FileInputStream(f);

/* Open our is as an AudioStream */
AudioInputStream ais = AudioSystem.getAudioInputStream(is);

/* Put out audio input stream into our clip */
clickClip.open(ais);

/* Play the clip */
clickClip.start();

Hope it helps.

sure you can and folow how to:

Clip clickClip = AudioSystem.getClip();

/* Create a URL that points to our file */
File f=new File("C:/WINDOWS/Media/CHIMES.wav");
InputStream is=new FileInputStream(f);

/* Open our is as an AudioStream */
AudioInputStream ais = AudioSystem.getAudioInputStream(is);

/* Put out audio input stream into our clip */
clickClip.open(ais);

/* Play the clip */
clickClip.start();

Hope it helps.

Thank YOU!! do I need to import anything to make it work?

You could also create your own class that mimics the Scanner class's functionality; you'd basically include a constructor that takes a File Object, and you'd implement your own next() methods, etc... in those methods you could call the Scanner methods and also implement your own custom functionality (calling the AudioClip)

Yes:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
//AND TROW THOSE EXCEPTIONS
import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

And throw the exceptions
hope it helps.

So I was able to get the code to work THANK YOU! but when I try to play a 20kb zelda(video game theme sound).wav file it compiles but doesnt play...any thoughts? The system files play fine.

import java.io.*;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
//AND TROW THOSE EXCEPTIONS
import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Sound
{
public void playSound()
{
try{
        Clip clickClip = AudioSystem.getClip(); 
 
        /* Create a URL that points to our file */
        File f=new File("C:/Users/caldwell44/Desktop/zelda_07");
        InputStream is=new FileInputStream(f);
        
        /* Open our is as an AudioStream */
        AudioInputStream ais = AudioSystem.getAudioInputStream(is);
        
        /* Put out audio input stream into our clip */
        clickClip.open(ais);
        
        /* Play the clip */
        clickClip.start();

    }
        catch(LineUnavailableException e){}
        catch(FileNotFoundException e){}
        catch(UnsupportedAudioFileException e){}
        catch(java.io.IOException e){}
}

Thanks for all the help guys!
james

So I was able to get the code to work THANK YOU! but when I try to play a 20kb zelda(video game theme sound).wav file it compiles but doesnt play...any thoughts? The system files play fine.

import java.io.*;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
//AND TROW THOSE EXCEPTIONS
import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Sound
{
public void playSound()
{
try{
        Clip clickClip = AudioSystem.getClip(); 
 
        /* Create a URL that points to our file */
        File f=new File("C:/Users/caldwell44/Desktop/zelda_07");
        InputStream is=new FileInputStream(f);
        
        /* Open our is as an AudioStream */
        AudioInputStream ais = AudioSystem.getAudioInputStream(is);
        
        /* Put out audio input stream into our clip */
        clickClip.open(ais);
        
        /* Play the clip */
        clickClip.start();

    }
        catch(LineUnavailableException e){}
        catch(FileNotFoundException e){}
        catch(UnsupportedAudioFileException e){}
        catch(java.io.IOException e){}
}

Thanks for all the help guys!
james

ANSWERED MY QUESTION! apparently you can only play PCM .wav files! That was 2hr of my life i won't get back LOL!

Is there a way to make the scanner wait 5seconds before it executes the terminate code?

done=true;

I am trying to play a short sound but it terminates the program before it can fully play. Im assuming it would be in mill seconds.

Hi James;
I got some few questions:
id this constante a file or folder:

"C:/Users/caldwell44/Desktop/zelda_07"

In fact you should receive FileNotFoundException but you do not due to this:

catch(java.io.IOException e){);

You catch the exception without reacting.
try instead that:

catch(java.io.IOException e){System.out.println("IOException");}

and let me now.

hope it helps.

actually it works fine the way I have it, uhm when I use this last code you gave me it shows:
"java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at GameTest.main(GameTest.java:27)
"
at the bottom of BlueJ's scanner window while the game is running. Do you know how to pause the the termination command so that I can allow the "game over" screen to play its sound first?

else if(option==5)
            {
                //sound
                mySound.playGameOver();
                //I need to put code here to delay done=true for 5 seconds...
                done=true;

The file I was refering to in the "C:/Users/caldwell44/Desktop/zelda_07""C:/Users/caldwell44/Desktop/zelda_07"
was not in PCM .wav format so that is why it didn't play.
I am saving my Game of life program on my school's drive, that's why it looks the way it does. I have put mario .wav files to the different options that the user pics just as an added feature to the game.

Well the answer to my question of delaying code is as follows:

else if(option==5)
            {
                 mySound.playGameOver();
                 int t;
                    for(t=0; t<5; t++)
                    {
                        try
                          {
                            Thread.sleep(750);    //pauses done=true for +-750mill sec
                            }catch (InterruptedException ie){}
                    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.