Java _ How to process waveform files (playback. Au Audio File URL is converted into waveform output)

Source: Internet
Author: User
Tags file url
Java tip 24: how to play audio files in an application
A step-by-step wizard for playing sound Programs in Java applications

Chong ser Wah
John D. Mitchell
Introduction
The existing support for sound playback in Java Applet makes the sound playback task very simple, but this support is missing in applications. This tip tells you how to play audio files in a Java application. (650 words)
Playing audio files in Java applications is not officially supported in the existing Java version, but don't worry, we still have a solution. This tips will show you how to do it-Let's start with the basic steps required to play a sound file in the applet, and then go slowly to the Java application's support.
Playing a sound file in an applet is very simple and generally requires the following steps:


Create an audioclip object
Mount the. Au sound file to the audioclip object.
One-time playback or continuous playback
Stop playing
The following code is used:

Import java. Applet .*;

Audioclip AC = getaudioclip (getcodebase (), soundfile );
AC. Play (); // play once
AC. Stop (); // stop playing
AC. Loop (); // play continuously


It seems that playing sound files with the same code in Java applications is also feasible. But unfortunately, if you do, the compiler will report an error. Why? Because the audioclip object and its method getaudioclip () belong to the java. Applet package-not the application package. Fortunately, we can also make some changes on our own to implement the sound playback function.

The trick to solve this problem is to take advantage of some unreceivented features provided by Sun and its JDK. Take a look at the file classes.zip in Sun JDK (use any decompression tool), and find that not only standard Java packages such as Java. the applet also contains the sun. audio. (In the sun/audio directory .)

The sun. Audio package contains everything you need to play a sound file! The following is the sample code:

Import sun. Audio. *; // import the sun. Audio package
Import java. Io .*;

// ** Add this into your application code as appropriate

// Open an input stream to the audio file.
Inputstream in = new fileinputstream (filename );

// Create an audiostream object from the input stream.
Audiostream as = new audiostream (in );

// Use the static class member "Player" from class audioplayer to play
// Clip.
Audioplayer. Player. Start ();

// Similarly, to stop the audio.
Audioplayer. Player. Stop ();


If you want to use a URL as the source of the sound stream, replace the input stream with the following code to create the sound stream:

Audiostream as = new audiostream (URL. openstream ());


If you need to play audio files continuously, you need to be slightly more complex:

// Create audio stream as discussed previusly.

// Create audiodata source.
Audiodata DATA = As. getdata ();

// Create continuousaudiodatastream.
Continuousaudiodatastream CAS = new continuousaudiodatastream (data );

// Play audio.
Audioplayer. Player. Play (CAS );

// Similarly, to stop the audio.
Audioplayer. Player. Stop (CAS );



Do you know how to do it now? Don't forget, this technique uses the features that are not written into the document, so it is not guaranteed to work properly under any circumstances, but it is no problem to use sun's JDK.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.