Play .wav music file

Y

yingjian.ma1955

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();
}}
 
A

Andrew Thompson

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

Oliver Wong

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
 
A

Andrew Thompson

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

yingjian.ma1955

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

Oliver Wong

Andrew Thompson said:
Oliver Wong wrote:
...

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
 
Y

yingjian.ma1955

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
(e-mail address removed).

Thank you very much.
 
Y

yingjian.ma1955

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
(e-mail address removed).

Thank you very much.
 
L

Luc The Perverse

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
(e-mail address removed).

Thank you very much.

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

yingjian.ma1955

Thanks a lot. You are so kind. If you know a music file that also
works, please let me know.
 
A

Andrew Thompson

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

Oliver Wong

Andrew Thompson said:
(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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top