Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Playing sound in my nursing program

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Playing sound in my nursing program

    Well, I just joined this forum today. I am a nursing student who is trying to use Java as a way to make my nursing education more interesting, so I decided to create a program to help me and others study nursing. Well, I hit the wall very hard today. Java has been so amazingly fun to do, until I decided to add a bit of music into my code. Before I asked anyone, I searched google and found this code here:

    package music4;
    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 Music4
    {
    public static void main(String[]args)
    {
    String filename="lion2.wav"; (Here is where I name the file. Why is returning to me it cant find it. Please help)

    int EXTERNAL_BUFFER_SIZE = 524288;

    File soundFile = new File(filename);

    if (!soundFile.exists())
    {
    System.err.println("Wave file not found: " + filename);
    return;
    }

    AudioInputStream audioInputStream = null;
    try
    {
    audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    return;
    }

    AudioFormat format = audioInputStream.getFormat();

    SourceDataLine auline = null;

    //Describe a desired line
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    try
    {
    auline = (SourceDataLine) AudioSystem.getLine(info);

    //Opens the line with the specified format,
    //causing the line to acquire any required
    //system resources and become operational.
    auline.open(format);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    return;
    }

    //Allows a line to engage in data I/O
    auline.start();

    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try
    {
    while (nBytesRead != -1)
    {
    nBytesRead = audioInputStream.read(abData, 0, abData.length);
    if (nBytesRead >= 0)
    {
    //Writes audio data to the mixer via this source data line
    //NOTE : A mixer is an audio device with one or more lines
    auline.write(abData, 0, nBytesRead);
    }
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    return;
    }
    finally
    {
    //Drains queued data from the line
    //by continuing data I/O until the
    //data line's internal buffer has been emptied
    auline.drain();

    //Closes the line, indicating that any system
    //resources in use by the line can be released
    auline.close();
    }
    }
    }

    I can't seem to make the program find the location of my file. No matter what I do it returns can't find the file. I copied into the same directory as the freakin program, and it still won't locate it. I hope I am over looking something very simplistic.

    Please help me, I am very desperate in getting this part of my program to work.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Playing sound in my nursing program

    You can check exactly where the file is being looked for with

    if (!soundFile.exists())
    {
        System.err.println("Wave file not found: " + soundFile.getAbsoluteFile());
        return;
    }

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Playing sound in my nursing program

    Thank you so much

  4. #4
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Playing sound in my nursing program

    Damn, the problem is the music plays while my program stops. I Kind of want it to play in the backround of the program. Any quick tips.

Similar Threads

  1. Need help playing sound in backround of a program in a loop.(Please Fix my code!)
    By DusteroftheCentury in forum What's Wrong With My Code?
    Replies: 30
    Last Post: January 21st, 2012, 02:14 AM
  2. Replies: 0
    Last Post: January 12th, 2012, 03:54 PM
  3. Adding sound to program.
    By Archibold9 in forum What's Wrong With My Code?
    Replies: 32
    Last Post: December 21st, 2011, 02:28 PM
  4. Playing sound in an applet
    By Skyhigh32 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 3rd, 2011, 08:41 AM
  5. playing sound using java
    By sdhilipan89 in forum Java Applets
    Replies: 6
    Last Post: March 25th, 2010, 09:59 AM