Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Play .wav music file

Thread view: 
yingjian.ma1955@gmail.com - 14 Jul 2006 20:02 GMT
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();
  }}
Andrew Thompson - 14 Jul 2006 20:32 GMT
...
> 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.
yingjian.ma1955@gmail.com - 14 Jul 2006 21:08 GMT
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.
yingjian.ma1955@gmail.com - 17 Jul 2006 04:25 GMT
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
yingjian.ma1955@gmail.com.  

Thank you very much.
yingjian.ma1955@gmail.com - 17 Jul 2006 04:26 GMT
yingjian.ma1...@gmail.com 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
> yingjian.ma1955@gmail.com.  
>
> Thank you very much.
Luc The Perverse - 17 Jul 2006 05:33 GMT
>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
> yingjian.ma1955@gmail.com.
>
> Thank you very much.

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

--
LTP

:)
yingjian.ma1955@gmail.com - 17 Jul 2006 11:20 GMT
I tried some.  But they do not work with my code.
> >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
[quoted text clipped - 9 lines]
>
> :)
Andrew Thompson - 17 Jul 2006 13:32 GMT
( 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.
yingjian.ma1955@gmail.com - 17 Jul 2006 14:42 GMT
Thanks a lot.  You are so kind.  If you know a music file that also
works, please let me know.
Andrew Thompson - 17 Jul 2006 19:50 GMT
> 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.
Oliver Wong - 18 Jul 2006 14:49 GMT
>> Thanks a lot.  You are so kind.  If you know a music file that also
>> works, please let me know.
[quoted text clipped - 4 lines]
> 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 - 14 Jul 2006 20:40 GMT
> 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
Andrew Thompson - 14 Jul 2006 21:08 GMT
...
>     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.
Oliver Wong - 14 Jul 2006 22:46 GMT
> ...
>>     The .WAV file format is a container format. That is, it contains
[quoted text clipped - 17 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.