Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the sound processing method of JAVA?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the sound processing method of JAVA". In the daily operation, I believe that many people have doubts about the sound processing method of JAVA. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the sound processing method of JAVA?" Next, please follow the editor to study!

There are many kinds of digital audio formats, and their quality is related to sampling frequency and sampling accuracy. The frequency is expressed in Hz, which represents the number of samples per second. The higher the sampling frequency, the better the sound quality. The sampling accuracy is the amount of data stored per sampling, which determines the number of discrete amplitudes that each digital signal can represent. The more data stored in each sample, the better the sound quality. But high-quality sound takes up a lot of memory and disk space. Considering the network bandwidth, it takes a long time to transmit over an Internet connection. For Applet, it is extremely important to keep sound files to a minimum.

Java can support the following four sound formats:

◆ AU

◆ AIFF

◆ WAVE

◆ MIDI

The first sound format, AU, is the only sound format supported by previous versions of Java 1.1. The sampling frequency is 8000 Hz and the sampling precision is 8 bits. AIFF and WAVE, like the AU format, are used to represent digital sound. Among them, the WAVE format provides a wider range of sound quality. The MIDI format is dedicated to music and describes sound in terms of notes and instruments rather than digital sounds.

About sound creation: is the sound key tool a standard application in Windows9X? A tape recorder. The recording format is WAVE. In addition, there are many professional applications, sampling and editing digital sound, passing conversion.

Resource requirements:

■ hardware resource requirements:

⑴ sound card

⑵ speaker

# if you need to record sound, you need a microphone.

■ software resource requirements:

⑴ windows9X operating system

⑵ web browser

⑶ Java plug-in / Runtime Environment

Treatment plan one

Starting with JAVA2, the Java language can support the above four sound formats. The simple way is to load the sound in java through the AudioClip () interface of the Applet class. This interface provides common methods for sound clips and has minimal support for playing sound clips.

The interface defines the following methods:

■ play ()

Syntax: void play ()

Function: start playing sound clips.

Whether the sound clip is already playing or not, play it from the beginning. Play it once but not repeat it.

■ loop ()

Syntax: void loop ()

Function: play sound clips in a loop.

This method is called to play from scratch regardless of whether the sound segment is already playing or not.

■ stop ()

Syntax: void stop ()

Function: stop playing sound clips.

AudioClip interface can obtain sound fragments and sound fragments through getAudioClip () and getCodeBase () methods.

URL address. You can use this method to play a specified sound clip in a WEB page.

# requirements: based on the development environment of JDK1.2 or above. If you only play sound files in AU format, JDK1

.1 is fine.

Attached: soundtest.html source code

"HTML"

"HEAD"

"TITLE"

HTML Test Page

"/ TITLE"

"/ HEAD"

"BODY"

"APPLET

CODEBASE = "" CODE = "test.class" NAME = "TestApplet" WID

TH = 400HEIGHT = 200HSPACE = 0 VSPACE = 0 ALIGN = mi

Ddle "

"PARAM NAME =" clip "VALUE =" sound.AU ""

"/ APPLET"

"/ BODY"

"/ HTML"

Attached: test,java source code

Package SoundTest

Import java.awt.*

Import java.awt.event.*

Import java.applet.*

Import javax.swing.*

Import java.net.*

Public class test extends JApplet {

Boolean isStandalone = false

String sound

JButton jButtonPlay = new JButton ()

JButton jButtonLoop = new JButton ()

JButton jButtonStop = new JButton ()

JLabel jLabel1 = new JLabel ()

AudioClip clip

/ / Get a parameter value

Public String getParameter (String key, String def) {

Return isStandalone? System.getProperty (key, def):

(getParameter (key)! = null? GetParameter (key): def)

}

/ / Construct the applet

Public test () {

}

/ / Initialize the applet

Public void init () {

Try {

JbInit ()

}

Catch (Exception e) {

E.printStackTrace ()

}

}

/ / Component initialization

Private void jbInit () throws Exception {

JButtonPlay.setText ("Play")

JButtonPlay.setBounds (new Rectangle (50,85,80,40))

JButtonPlay.addMouseListener (new java.awt.event.MouseAdapter () {

Public void mouseClicked (MouseEvent e) {

JButtonPlay_mouseClicked (e)

}

});

This.setSize (new Dimension (400200))

This.getContentPane () setLayout (null)

JButtonLoop.setText ("Loop")

JButtonLoop.setBounds (new Rectangle (150,85,80,40))

JButtonLoop.addMouseListener (new java.awt.event.MouseAdapter () {

Public void mouseClicked (MouseEvent e) {

JButtonLoop_mouseClicked (e)

}

});

JButtonStop.setText ("Stop")

JButtonStop.setBounds (new Rectangle (250,85,80,40))

JButtonStop.addMouseListener (new java.awt.event.MouseAdapter () {

Public void mouseClicked (MouseEvent e) {

JButtonStop_mouseClicked (e)

}

});

JLabel1.setText ("Sound Test Demo")

JLabel1.setBounds (new Rectangle (109,28,186,28))

This.getContentPane () .add (jButtonPlay null)

This.getContentPane () .add (jButtonStop null)

This.getContentPane () .add (jButtonLoop null)

This.getContentPane () .add (jLabel1 null)

Try {sound = this.getParameter ("clip", "sound.AU");} catch (Exce

Ption e) {e.printStackTrace ();}

If (soundproof null) {

Try {

Clip=JApplet.newAudioClip (new URL (getCodeBase (), sound))

}

Catch (MalformedURLException e) {

System.out.println ("Bad URL")

}

}

}

/ / Get Applet information

Public String getAppletInfo () {

Return "Applet Information"

}

/ / Get parameter info

Public String [] getParameterInfo () {

String [] pinfo =

{

{"clip", "String", "sound.AU"}

}

Return pinfo

}

Void jButtonPlay_mouseClicked (MouseEvent e) {

Clip.play ()

}

Void jButtonLoop_mouseClicked (MouseEvent e) {

Clip.loop ()

}

Void jButtonStop_mouseClicked (MouseEvent e) {

Clip.stop ()

}

}

Treatment plan 2

Use Sound API in the JAVA media framework to process sound clips. Sound API is included in SUN's JAVA media framework, as well as in the JDK1.3 version. Compiling source code requires a development environment and run-time environment that supports JDK1.3.

You can use Sound API to add Applet to the web page to meet the requirements. However, browsers need to be supported by Java plug-ins or runtime environments.

Sound API mainly consists of the following four parts:

Packages

Javax.sound.midi provides MIDI (Musical Instrument Digital Interface) iMaple O,

Sequences, synthetic interfaces, and classes.

Javax.sound.midi.spi Supplies interfaces for service providers to impl

Ement when offering new MIDI devices, MIDI file readers and writers, o

R sound bank readers.

Javax.sound.sampled provides interfaces and classes for the capture, processing and playback of sampled digital audio.

Javax.sound.sampled.spi Supplies abstract classes for service provider

S to subclass when offering new audio devices, sound file readers and

Writers, or audio format converters.

Treatment plan 3

Leverage the sound class in JAVA 3D. An abstract definition of this similar sound resource. The sound method can be applied to each specific sound object.

The class relationship of the Sound class is as follows:

Java.lang.Object

+-javax.media.j3d.SceneGraphObject

+-javax.media.j3d.Node

+-javax.media.j3d.Leaf

+-javax.media.j3d.Sound

The subclasses of the sound class are:

⒈ BackgroundSound

⒉ PointSound

⒊ ConeSound

The steps to use the above three sounds are as follows:

⒈ defines and generates a MediaContainer object while providing the sound files required by the object, usually

To set up a separate directory for AU/WAV sound files.

⒉ defines a BackgroundSound/ PointSound/ ConeSound object and sets the sound as needed.

Whether the sound cycle, intensity and other parameters, set its scope of action and setEnable to make it work.

⒊ sets the coordinate system based on specific objects.

# JAVA 3D applications can only play sound files in au/wav format. Cannot play midi file

At this point, the study on "what is the sound processing method of JAVA" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report