1. Play the sound in the Applet
Playing sound is an important part of Java's support for multimedia.
Currently, popular audio formats include WAV, mid, and AU audio files with extensions.
Java now mainly supports the voice in the name of AU.
In applet, we provide audio-like clip.
Since the multimedia functions of the Applet are clearly described in the book, it is also very simple.
Let's end this section with an example.
1 Import Java. Applet .* ; 2 Public Class Sounda Extends Applet 3 { 4 Public Static Void Main (string [] ARGs) 5 { 6 Audioclip AC = getaudioclip (getcodebase (), "sloop. Au" ); 7 AC. Play (); 8 // AC. Loop (); 9 // AC. Stop (); 10 } 11 }
The above example is a simple description. audioclip is a playback class in Java.
Java provides two useful methods: getcodebase (),
Getdocumentbase () they return the applet itself and the URL that contains the applet home page.
The above sloop. Au is the sound file on my machine.
Here we directly use the play method of audioclip to play the sound.
You can test the image function of the applet by adding a button.
Java applicationsProgramPlaying audio in
Because the audioclip class is not supported in the application.
Therefore, playing audio files has become a major challenge.
Can we not support sound when making our own Java player or app media Java applications?
No,
Sun provides a new Java package for voice support in applications.
Sun. Audio provides most of the required audio processing classes in this package.
Note: There are many interesting technical support in the sun package,
Such as java2d and tools,
You can try it on your own.
Let's test the sound playback function.
1 Import sun. Audio .* ; 2 Import java. Io .* ; 3 Public Class Sound 4 { 5 Public Static Voie main (string ARGs []) 6 { 7 Try { 8 Fileinputstream fileau = New Fileinputstream ( " Sloop. au " ); 9 Audiostream As = New Audiostream (fileau ); 10 Audioplayer. Player. Start ( As ); 11 } 12 Catch (Exception e ){} 13 } 14 }
Note: The above two examples do not provide the corresponding stop method,
BenArticleIndicates a method attempt,
Not a comprehensive article,
However, the above examples are all available.
3. Application of sound package in javax
When I perform the above test,
I found that the javax directory also provides a sound package with powerful support for Java sound,
In addition, a large number of examples can be downloaded from the jjse on the sun website.
If you are interested in voice processing, download and try,
Now there are a lot of introductions on how to play music in Java applets for users to browse pages and enjoy it,
However, there are few introductions on how to play music in an application,
Next I will explain how to implement it gradually.
Jun when the previous Java Development Kit Version 1.1.3 does not officially support playing music files in the application,
However, this can still be implemented.
First, let's review the four steps of playing music in the applet,
CodeAs follows:
Import java. Applet .*; Audioclip AC= Getaudioclip (getcodebase (), file );//Create an audioclip object and install the. Au file.AC. Play ();//Play onceAC. Stop ();//Stop, stop, and playAC. Loop ();//Loop playback
You may think this code can also be used in the application,
But unfortunately, it will cause errors during compilation.
Because both the audioclip object and the getaudioclip () method belong to the java. Applet package, which cannot be called in the application.
To solve the problem, use sun to publish it in JDK but do not officially indicate the characteristics of (untitled ented.
Use WinZip or zip class.zip to view audio files. In addition to processing audio files in the Java. Applet package,
The sun. Audio package in the/Sun/audio directory also provides similar methods.
The following is the implementation code and comments:
Import sun. Audio .*;//Import the sun. Audio packageImport java. Io .*; InputstreamIn=NewFileinputstream (filename );//Open a sound file stream as inputAudiostreamAs=NewAudiostream (In);//Create an audiostream object using the input streamAudioplayer. Player. Start (As);//"Player" is a static voice player in audioplayer used for controlling broadcast and playback.Audioplayer. Player. Stop (As);
To download and play a file from the Internet, use the following code to open the Music File URL:
Audiostream as = new audiostream (URL. openstream ());
Playing a continuous sound stream is complicated:
First, create a sound stream;
Audiodata DATA = As. getdata ();//Create an audiodata SourceContinuousaudiodatastream cas=NewContinuousaudiodatastream (data); audioplayer. Player. Play (CAS );//Sound releasingAudioplayer. Player. Stop (CAS );//Stop
Finished! Because the above program uses undisclosed features, it cannot be guaranteed that it can run on a compiler other than the current version of Sun JDK.