Play .wav music file

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

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.

Advertisements

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.

Advertisements

<> 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.

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.

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.

"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.

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.

<> 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.
>

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

Guest

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 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/

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!