Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Play .wav music file

Reply
Thread Tools

Play .wav music file

 
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      07-14-2006
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();
}}

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      07-14-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      07-14-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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/tutor...d/playing.html

- Oliver

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      07-14-2006
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.

 
Reply With Quote
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      07-14-2006

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.

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      07-14-2006

"Andrew Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

 
Reply With Quote
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      07-17-2006
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 Removed).

Thank you very much.

 
Reply With Quote
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      07-17-2006

(E-Mail Removed) 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
> (E-Mail Removed).
>
> Thank you very much.


 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      07-17-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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 Removed).
>
> Thank you very much.
>


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

--
LTP




 
Reply With Quote
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      07-17-2006
I tried some. But they do not work with my code.
Luc The Perverse wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >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 Removed).
> >
> > Thank you very much.
> >

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To Play ".wave" file with C++(Background music) andrew.smith.cpp@gmail.com C++ 7 05-09-2008 12:08 PM
To Play ".wave" file with C++(Background music) andrew.smith.cpp@gmail.com C++ 4 05-09-2008 12:02 PM
Play a Music File Tj Superfly Ruby 3 12-14-2007 10:51 PM
DVD/CD plays music but won't play a dvd =?Utf-8?B?QmVsaW5kYUY=?= Microsoft Certification 3 07-25-2005 10:02 AM
Saving website music / identify music berksposter Computer Support 6 10-19-2004 03:29 AM



Advertisments