Play .wav music file

Discussion in 'Java' started by yingjian.ma1955@gmail.com, Jul 14, 2006.

  1. Guest

    Hi,

    I have a program to play a .wav music. But it plays a broken music.
    When I play it using Real Player, it is OK. How can I fix it and why
    does it play a broken music? You can use your .wav file to try it.
    Thanks a lot.

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import javax.swing.*;
    public class Music extends JFrame {
    public Music() {
    super("Music");
    setSize(190, 80);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = getContentPane();
    FlowLayout flo = new FlowLayout();
    content.setLayout(flo);
    AButton p = new AButton();
    content.add(p);
    setContentPane(content);
    setVisible(true);
    }
    public static void main(String[] arguments) {
    Music m = new Music();
    }
    }
    class AButton extends JButton implements Runnable, ActionListener {
    AudioClip[] a = new AudioClip[1];
    Thread runner;
    AButton() {
    super("Play");
    addActionListener(this);
    try {
    URL u = new URL("file:ladieu.wav");
    a[0] = JApplet.newAudioClip(u);
    } catch (MalformedURLException e) { }
    }
    public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (command == "Play")
    startMusic();
    if (command == "Stop")
    stopMusic();
    }
    void startMusic() {
    if (runner == null) {
    runner = new Thread(this);
    runner.start();
    setText("Stop");
    }
    }
    void stopMusic() {
    a[0].stop();
    runner = null;
    setText("Play");
    }
    public void run() {
    a[0].loop();
    }}
     
    , Jul 14, 2006
    #1
    1. Advertising

  2. wrote:
    ....
    > I have a program to play a .wav music. But it plays a broken music.


    No it does not.*

    > When I play it using Real Player, it is OK. How can I fix it and why
    > does it play a broken music? You can use your .wav file to try it.


    Before I get back into it, I just want to comment that was a nice
    code example you put, if you'd allowed a text field to name the
    ..WAV it would have been even better, but otherwise it did
    compile and run first go. Excellent!

    Now to get back to the problem..

    * I tried your code with a WAV that I have here, and it played
    just fine. I suspect the problem is not your code but the
    WAV file itself.

    The fact that RealPlayer can play your WAV OK does
    /not/ confirm that the WAV is of a correct format.

    Most general purpose sound players have all kinds of
    smarts to correct common problems in sound files,
    because many sound file editors write ..crappy files -
    basically.

    Java, on the other hand, assumes the sound files are written
    correctly (which requires a lot less coding). This is necessary
    to avoid API bloat, but it means some sound files (WAV, AU
    & MID) just cannot cannot be played using core Java.

    The JMF might have a bit more smarts, but that is
    another story. **

    You might try a variety of things to fix the problem.

    If you control all the sound files that you want to play, it
    is easier if you reload them in a better sound editor and
    save them as a new name - the sound editor might
    correct the problem and give you a file that Java can read.

    ** If you want to be able to play many WAV types, you first
    might look at JMStudio from the JMF - more to check that
    the JMF can load and play the files of interest (though even
    it does not handle all formats or encodings).

    If JMF can play the sound files that core Java cannot -
    use JMF instead.

    HTH

    Andrew T.
     
    Andrew Thompson, Jul 14, 2006
    #2
    1. Advertising

  3. Oliver Wong Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > I have a program to play a .wav music. But it plays a broken music.
    > When I play it using Real Player, it is OK. How can I fix it and why
    > does it play a broken music? You can use your .wav file to try it.
    > Thanks a lot.


    The .WAV file format is a container format. That is, it contains audio
    chunks, but does not specify how those chunks might be encoded. They could
    be encoded in Microsoft G.723.1, MPEG Layer-3 or PCM, just to name a few
    examples. As such, a program can only play the WAV file if it also knows how
    to decode the underlying audio data.

    Sun's basic implementation only allows for playback of PCM encoded WAV
    files, though you can add plugins (or "providers" as Sun calls them) to add
    support to other encoding systems. See the "Common Problems" section of
    http://java.sun.com/docs/books/tutorial/sound/playing.html

    - Oliver
     
    Oliver Wong, Jul 14, 2006
    #3
  4. Oliver Wong wrote:
    ....
    > The .WAV file format is a container format. That is, it contains audio
    > chunks, but does not specify how those chunks might be encoded. They could
    > be encoded in Microsoft G.723.1, MPEG Layer-3 or PCM, just to name a few
    > examples. As such, a program can only play the WAV file if it also knows how
    > to decode the underlying audio data.


    The thing is (and I might be wrong here), I understand that
    if Java understands a format/encoding, it will load the sound,
    if not - it won't load it at all.

    Or to put that another way, Java will *not* load an unfamiliar
    format/encoding then play it incorrectly.

    This leads me to believe that the OP's problem is that the
    WAV file itself is corrupt.

    Do I understand wrong?

    Andrew T.
     
    Andrew Thompson, Jul 14, 2006
    #4
  5. Guest

    Thank you for the quick answer. I tried it on many PCs. The only one
    that can play it OK is an old and slow PC, It is an iBuddy Desknote.
    That is why I thought Java had a problem. I'll try other sound file
    later.
     
    , Jul 14, 2006
    #5
  6. Oliver Wong Guest

    "Andrew Thompson" <> wrote in message
    news:...
    > Oliver Wong wrote:
    > ...
    >> The .WAV file format is a container format. That is, it contains
    >> audio
    >> chunks, but does not specify how those chunks might be encoded. They
    >> could
    >> be encoded in Microsoft G.723.1, MPEG Layer-3 or PCM, just to name a few
    >> examples. As such, a program can only play the WAV file if it also knows
    >> how
    >> to decode the underlying audio data.

    >
    > The thing is (and I might be wrong here), I understand that
    > if Java understands a format/encoding, it will load the sound,
    > if not - it won't load it at all.
    >
    > Or to put that another way, Java will *not* load an unfamiliar
    > format/encoding then play it incorrectly.
    >
    > This leads me to believe that the OP's problem is that the
    > WAV file itself is corrupt.
    >
    > Do I understand wrong?


    I don't know. I haven't played with the Java sound API that much. As a
    quick test, I ran the OP's program, pointed it to a sound file which did
    *NOT* exist, ran it, and clicked "play". And nothing happened besides the
    button label changing from "play" to "stop". Specifically, no exceptions
    were thrown or anything like that. I didn't bother to test with a non-PCM
    encoded WAV file as I don't have speakers on this computer. So if you
    expected an exception to be thrown if something goes wrong... well, it looks
    like Java doesn't do that.

    - Oliver
     
    Oliver Wong, Jul 14, 2006
    #6
  7. Guest

    I tried a few wav files. They are all broken. Can you send me a small
    (say <30kb) music file of any type (does not have to be wav but not
    midi) that I can use with my code? My email is
    .

    Thank you very much.
     
    , Jul 17, 2006
    #7
  8. Guest

    Oops, I mean < 30 Mb

    wrote:
    > I tried a few wav files. They are all broken. Can you send me a small
    > (say <30kb) music file of any type (does not have to be wav but not
    > midi) that I can use with my code? My email is
    > .
    >
    > Thank you very much.
     
    , Jul 17, 2006
    #8
  9. <> wrote in message
    news:...
    >I tried a few wav files. They are all broken. Can you send me a small
    > (say <30kb) music file of any type (does not have to be wav but not
    > midi) that I can use with my code? My email is
    > .
    >
    > Thank you very much.
    >


    Huh? Wave files are all over the internet - just go grab one

    --
    LTP

    :)
     
    Luc The Perverse, Jul 17, 2006
    #9
  10. Guest

    I tried some. But they do not work with my code.
    Luc The Perverse wrote:
    > <> wrote in message
    > news:...
    > >I tried a few wav files. They are all broken. Can you send me a small
    > > (say <30kb) music file of any type (does not have to be wav but not
    > > midi) that I can use with my code? My email is
    > > .
    > >
    > > Thank you very much.
    > >

    >
    > Huh? Wave files are all over the internet - just go grab one
    >
    > --
    > LTP
    >
    > :)
     
    , Jul 17, 2006
    #10
  11. wrote:

    ( Please refrain from top-posting - I find it very confusing )

    > I tried some. But they do not work with my code.


    I just tested these two - they work with your code..
    <http://www.physci.org/test/oscilloscope/sounds/>

    Andrew T.
     
    Andrew Thompson, Jul 17, 2006
    #11
  12. Guest

    Thanks a lot. You are so kind. If you know a music file that also
    works, please let me know.
     
    , Jul 17, 2006
    #12
  13. wrote:
    > Thanks a lot. You are so kind. If you know a music file that also
    > works, please let me know.


    (Uncompressed) WAV is not an optimal way to store music.

    If I wanted to record music, I would use something like
    MP3 format - which cannot be read by core Java (it
    requires the JMF), or going in a different direction, a
    MIDI file, which has less predictable output quality, but
    is *much* smaller.

    Andrew T.
     
    Andrew Thompson, Jul 17, 2006
    #13
  14. Oliver Wong Guest

    "Andrew Thompson" <> wrote in message
    news:...
    > wrote:
    >> Thanks a lot. You are so kind. If you know a music file that also
    >> works, please let me know.

    >
    > (Uncompressed) WAV is not an optimal way to store music.
    >
    > If I wanted to record music, I would use something like
    > MP3 format - which cannot be read by core Java (it
    > requires the JMF)


    You're supposed to pay royalties to use the MP3 codec in your software.
    An alternative is the Ogg Vorbis format, which is free (both as in liberty
    and as in beer). There's plugins for JMF and a standalone (no JFM required)
    decoder at http://www.j-ogg.de/

    - Oliver
     
    Oliver Wong, Jul 18, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abacomedia
    Replies:
    12
    Views:
    1,214
    Hywel Jenkins
    Jun 15, 2004
  2. mutyala rao

    How to play MP3 / Wav file using C

    mutyala rao, Apr 26, 2006, in forum: C Programming
    Replies:
    12
    Views:
    17,666
    ruoqingmm
    Apr 29, 2006
  3. Scott
    Replies:
    2
    Views:
    2,661
    Scott
    Apr 30, 2007
  4. microteq

    Unable to play .wav file on WMP

    microteq, Jun 26, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    787
    jufemaiz: jc
    Jun 27, 2007
  5. '2+
    Replies:
    2
    Views:
    520
Loading...

Share This Page