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 12 of 12

Thread: how to make my code play some audio?Just a bit!

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default how to make my code play some audio?Just a bit!

    Hello everybody,

    i now that the 'policy' is to post the code and based on this talk.But i have not an idea about this,so no code is written by me?

    -So you came here without googling?
    Of course not!I searched the google but found complex programs,that i think that are for playing music,etc.

    So what i need is to just make a single 'beep' in my program.Can please someone help?

    PS- sorry for the post,but i really have no idea about audio and code


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to make my code play some audio?Just a bit!

    Quote Originally Posted by Samaras View Post
    ...-So you came here without googling?
    Of course not!I searched the google but found complex programs,that i think that are for playing music,etc....
    surely google has something to offer

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: how to make my code play some audio?Just a bit!

    Can't make it work :/ Maybe i am not ready for this :/

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: how to make my code play some audio?Just a bit!

    Only this piece of code was ok from compile
    public void soundTempo(){
            JFrame f = new JFrame("Tempo Dial");
            f.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {System.exit(0);}
            });
            f.getContentPane().add("Center", new TempoDial());
            f.pack();
            f.setSize(new Dimension(200,140));
            f.setVisible(true);
        }
    However i do not what it does.When i run it,i get this
    tempoDial-bmp.jpg
    i do not hear anything(yes,the audio is ok in my pc)

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: how to make my code play some audio?Just a bit!

    Quote Originally Posted by Samaras View Post
    ...
    So what i need is to just make a single 'beep' in my program...
    If you have a speaker on your system and just want to make it beep, you can try the following:
            System.out.println('\007');
    I'm talking about an old-fashioned "raw" speaker here, not a sound system attached to a sound card or motherboard sound card circuitry.

    Anyhow...

    It might or might not work, depending on your computer's setup.

    The '\007' character is ASCII BEL, which used to make the bell on a Teletype machine go "ding" so that an operator could know an important message had arrived (in case the rattling of the machine's mechanical print mechanism hadn't awakened him from his slumber).

    My Windows XP machine and my Centos 5.8 workstations (and lots of other PC operating system setups) don't have bells, but they cause a single "Beep" (a single audio tone with duration of a small number of hundreds of milliseconds) to be emitted from the speaker when you send this character to the standard output data stream.

    Potential problems:
    1. This is not part of any Java (or any other language) standard. It might or might not work with your machine

    2. Not all of my workstations have speakers. Interestingly, some of them have sound cards, and there might (or, maybe, not) be ways to set things up so that a system "beep" goes through the sound card. I won't get into this, since it won't add to anyone's Java learning experience.

    3. There is no way to control the frequency or duration (or any other characteristics) of the "beep." You get what you get (or not).


    Quote Originally Posted by Samaras
    have no idea about audio and code
    Now, if you don't know about sound and Java, you can look for tutorials. None of them will be about a single simple "beep" through the speaker. They will be about how to use some kind if audio API to get Java to emit audio through a sound card. They might be about reading a file and playing music, but you can read an audio .wav file in which a "beep" has been encoded and play it through your sound card also.

    When I started Java a couple of months ago I didn't have audio in mind, but I became interested (a little), so I looked at a couple of places:

    In addition to the well-traveled Java Tutorial Sound Trail, I found jsresources.

    There is a Simple Audio Player example that gives a complete program (using the javax.sound.sampled package) for playing an audio .wav file. On my Linux systems there are a number of "beep" audio .wav files in the /usr/share/sounds directory. I just played KDE_Beep_Beep.wav with this application and got something from my sound card that sounds like a beep. On my Windows XP machine there is a folder, C:\Windows\Media, that has lots of system .wav files. I don't see a beep (I didn't look very hard), but there is a "ding" and a "chord" and other .wav files that can be played with the Simple Audio Player that I reference above.

    Now, it might happen that the Simple Audio Player doesn't work "out of the box" on your system. There is a lot (a lot) of information on that site that might help. Sometimes if you want things to happen, you have to work for it. For example, that application uses settings already present in your sound system. The program doesn't set the volume. If sound is muted, this program won't turn it on. If you have more than one sound channel, it uses whatever has previously been set. Etc. There are ways to make different things happen, but just getting something out of the sound card will be a start, right?

    The next step might be to generate sampled data values to feed to the audio stream instead of having to read from a file. For something like a simple sinusoid, it's not too difficult to create a sequence of samples "on the fly" rather than relying on a pre-recorded file. This is an additional learning opportunity.

    Quote Originally Posted by Samaras
    Maybe i am not ready for this
    Well, what are you ready for? Learning about Java? Learning about audio? Learning about Java audio API's? What? I mean, no one was born knowing this stuff.

    The program that I referenced above has something like ninety lines of Java (after you get rid of all of those pesky copyright notices and the excellent javadoc stuff and other meaningful comments), and a lot of the code is concerned with exception handling rather than the relatively few lines needed to actually set things up to make sounds. I like that particular application as a learning vehicle since it doesn't use any special packages other than what is supplied with "standard" Java installations. Maybe it's a place to start. (If you are doing Android development, there's an entire media interface that does everything, but that's another story.)

    If anyone needs a .wav file to practice on, I have attached one that might be appropriate for August 25, 2012. (You have to unzip it, obviously.) The clip is from NASA and contains immortal words from a man who also said, "I am, and ever will be, a white-socks, pocket-protector, nerdy engineer."


    Cheers,

    Z
    Attached Files Attached Files
    Last edited by Zaphod_b; August 27th, 2012 at 01:59 PM.

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: how to make my code play some audio?Just a bit!

     System.out.println('\007');
    did not work.Just a square is printed and i am on winXP.However you wrote a big post,so thank you for your effort.I am going to focus on it.Thanks again!

  7. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: how to make my code play some audio?Just a bit!

    Quote Originally Posted by Samaras View Post
     System.out.println('\007');
    did not work.
    Well, I said that it might not make a beep. I am, however surprised that you got a square printed out.

    Here's what I did:
    created the following in a file named Z.java:
    public class Z
    {
        public static void main(String [] args)
        {
            System.out.println('\007');
        }
    }

    Compiled from a command line:

    javac Z.java

    Executed with a command line:
    java Z

    Nothing appeared in the command window, and I got a little "beep" from the built-in speaker (not through the sound card).

    My setup: Windows XP, Service Pack 3
    java and javac version 1.6.0_33

    Paste the code from this post into your editor (don't retype). Compile and execute from a command line. Do you still get a "square" printed out?

    Maybe it's a language environment thing. Perhaps you can try '\u0007' instead of '\007' in the println() statement.


    What happens if you echo Ctrl-G from a command line:

    Enter

    ECHO ^G

    Where ^G means hold the Ctrl key down while pressing the G key. (Reference Wikipedia - Bell Character)

    Cheers!

    Z
    Last edited by Zaphod_b; August 27th, 2012 at 08:59 PM.

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: how to make my code play some audio?Just a bit!

    I do not know how to handle java files from cmd.I use netbeans.
    '\u0007' did not word either.
    However i 'll focus on your 1st post. I feel bad for making you write so many words.Thanks again

  9. #9
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: how to make my code play some audio?Just a bit!

    Quote Originally Posted by Samaras View Post
    I do not know how to handle java files from cmd.I use netbeans.
    The Netbeans terminal emulator does not work very well with control characters (and, in particular doesn't work with '\007'). That's one reason I always start with command line programs and I always recommend that other people do it also if they expect consistent help from me. I really think that beginners shouldn't be isolated from underlying hardware and operating system functions.

    However, be that as it may...

    I tested the following procedure with Netbeans 7.0.1 on my Linux workstation. Let me know if it works on your system. (I don't have and don't intend to install Netbeans on my Windows box.)

    I made a new Netbeans project. I called it SpeakerBeep.

    Netbeans opened an editor window as SpeakerBeep.java.

    I left the package speakerbeep; statement in place and erased everything else and entered the simplest program that I could think of that plays a beep through the speaker from the Netbeans IDE (i.e. without depending directly on standard output through a terminal):

    Now SpeakerBeep.java looks like this:
    package speakerbeep;
    import java.awt.*;
     
    public class SpeakerBeep {
        public static void main(String [] args) {
            Toolkit.getDefaultToolkit().beep();
        }
    }

    I made SpeakerBeep the Main project and compiled. There were no errors.

    When I hit the Run icon, I got a beep from the speaker.


    In order to use the SimpleAudioPlayer source code from the jsresources link my earlier post, you can try the following:

    Create a new Netbeans project named SimpleAudioPlayer. It opens an edit window as SimpleAudioPlayer.java

    Leave the package simpleaudioplayer; statement in place and erase everything else. Paste SimpleAudioPlayer.java from the jsresources link into the edit window below the package statement. Make SimpleAudioPlayer the Main project and build the project. You should not get any errors.

    Now, you can change the statement where it gets the name of the file from a command line argument to a statement that it assigns the filename to a String that you define (give complete path/file name of a .wav file).

    Or...

    Set the project command line argument to the full path name of a .wav file (From Run menu, select Set Project Configuration->Customize... and fill in the "Arguments" box with the complete path/file name of a .wav file.)

    If you made changes, build the project again and make sure there are no errors.

    Then...

    Click the Run icon.

    When I did this on my system, the .wav file played through my sound card.

    IWFMYMMV. (It Works For Me; Your Mileage May Vary.)

    Cheers!

    Z
    Last edited by Zaphod_b; August 28th, 2012 at 03:44 PM.

  10. #10
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: how to make my code play some audio?Just a bit!

    1-the beep thing with the toolkit worked just fine

    2-the simpleAudioPlayer also works
    I played the notify file from C:\Windows\Media.However i got myself to download a random .wav file from here Wav Sounds The Best Answering Machine Wav Sounds,the first one that says rap.
    The file was stored at the Downloads file.I used the path
    "C:\\Documents and Settings\\User\\Τα έγγραφά μου\\Downloads\\rappin.wav"
    and got this
    run:
    javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    	at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1187)
    	at simpleaudioplayer.SimpleAudioPlayer.main(SimpleAudioPlayer.java:97)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    Is there a reason why the notify file played and the other did not?Is it because the one is downloaded and the other is stored by default in the system?Some connection to the sound card?

  11. #11
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: how to make my code play some audio?Just a bit!

    As mentioned in the javadoc comments for SimpleAudioPlayer: "Only PCM encoded files are supported. A-law, μ-law, ADPCM, ogg vorbis, mp3 and other compressed data formats are not supported." Many downloaded .wav files designed as ringtones or answer messages (including your hideous rappin.wav) are compressed with mp3. That's right, even though it is a .wav file, the audio data samples are encoded like an mp3 file. Won't work with SimpleAudioPlayer. Period.

    The jsresources web site has a FAQ and various places where they talk about Tritonus Plugin for Java JDK They also have an AudioPlayer example (that's AudioPlayer, not SimpleAudioPlayer) application that they say will handle mp3-encoded files. I haven't tried it and I probably won't, so feel free to experiment on your own.


    Cheers!


    Z
    Last edited by Zaphod_b; August 29th, 2012 at 04:18 PM.

  12. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Samaras (August 29th, 2012)

  13. #12
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: how to make my code play some audio?Just a bit!

    Zaphod_b i see.Thank you again for all your help.You are one of the most helpful and laconic people i have ever met in a forum.Too bad that i know you only from here.But too good that i know you at least from here.Thank you again,and i notify you that i am going to give you my first and only 'thanks' click!

Similar Threads

  1. I NEED HELP, HOW TO MAKE THIS CODE!!!
    By tahsim in forum Member Introductions
    Replies: 1
    Last Post: March 20th, 2012, 09:05 AM
  2. Getting audio stream to play from jar file instead of system directory?
    By nivangerow in forum What's Wrong With My Code?
    Replies: 26
    Last Post: October 5th, 2011, 09:51 AM
  3. i like to play this game with my phone and i wanted to make a program to help me
    By Imreallyawesome in forum What's Wrong With My Code?
    Replies: 25
    Last Post: August 12th, 2011, 07:34 PM
  4. How to play Audio file?
    By sush in forum Object Oriented Programming
    Replies: 3
    Last Post: June 29th, 2011, 08:11 AM
  5. Make my code follow MVC
    By alpvii in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 29th, 2010, 07:48 AM