Sound playback in Java applications

Source: Internet
Author: User

Sound can create artistic conception, trigger reverie, when combined with virtual image, more can make the whole world full of illusion, sound is the basis of multimedia technology.

Playing sounds is an important part of Java's support for multimedia, and it supports the following types of sound files:

Au-(with the extension AU or snd) applies to short sound files.

WAV-(with the extension WAV) is developed by Microsoft and IBM and can be played by almost any Windows application that supports sound.

AIFF-(Extended AIF or IEF) The audio Interchange file format is a standard audio file format that is common to Macintosh computers and Silicon Graphics (SGI) computers.

Midi-(extension mid) Musical Instrument Digital Interface MIDI is a standard that is recognized by the music industry and is primarily used to control devices such as synthesizers and sound cards.

So how do you implement sound file playback in a Java application?

Sun provides a Java package –sun.audio for voice support in the application, which provides most of the required sound processing classes under this package.

Let's do a play test:

Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;

Import sun.audio.*;

/**
* Test Sound Playback
*
* @author
*
*/
public class Musicdemo {

public static void Main (string[] args) throws Exception {
Create a music file input stream object
InputStream in = new FileInputStream ("Musics/flourish.mid");
Creating an audio Stream object
Final Audiostream Audiostream = new Audiostream (in);
New Thread (New Runnable () {
@Override
public void Run () {
Play a sound using an audio player
AudioPlayer.player.start (Audiostream);
}
}). Start ();

Thread.Sleep (3000);
Stop Sound Playback
AudioPlayer.player.stop (Audiostream);
}
}

Turn on the speaker, you can hear the sound, play after 3 seconds to stop the sound.

Attention:

When we write code in Eclipse, there may be an error message similar to "the type ' Audiostream ' is not API". In eclipse, we can see the Audiostream class in the Sun.audio package in the Rt.jar package.

Why does eclipse prompt us to "the type ' Audiostream ' is not API" error, which is actually a problem caused by Eclipse's setup.

To resolve this error, we just need to modify the eclipse settings a little bit: in preference->java->complier->errors/warning->deprecated and Restricted API in the Forbidden reference to change the error to warning can be.

The root causes are as follows:

The classes in J2SE can be broadly divided into the following packages: java.*,javax.*,org.*,sun.*. In addition to the "Sun" package, all other packages are standard implementations of the Java platform and will continue to be supported in the future. In general, packages such as "Sun" are not included in the standards of the Java platform, are related to the operating system, and are implemented differently in different operating systems (such as SOLARIS,WINDOWS,LINUX,MAC, and so on) and may change irregularly with the J2SE version. Therefore, the program code that calls the "sun" package directly is not a Java implementation of 100%.

That is, the java.*,javax.*,org.* package is part of the API public interface of J2SE, and if the program calls the APIs in these packages, the program can be run on all Java platforms, regardless of the operating system; sun.* The package is not part of the API's public interface, and the program that calls the "sun" package does not ensure that it works on all Java platforms, in fact, such programs may not work on future Java platforms.

Because of this, the classes in the sun.* package do not provide API documentation. Platform independence is one of the biggest advantages of the Java language, and Sun and Java licenses ensure that future API compatibility is maintained (except for those with serious bugs that are later modified). This compatibility means that your well-written program can still work in a future version of the class file.

Every vendor that implements the Java platform can use their own way. The classes in the sun.* package are the way Sun implements the Java platform, which works on the lower level of the JAVA2 SDK, which may not be supported by other Java platform developers. For example, if your Java program calls a class called "Sun.package.Foo", it is possible to generate "classnotfounderror" errors, and you will lose one of the main advantages of using Java.

Technically, it is not possible to prevent your program from calling classes in the sun.* package. In a version change, these classes may be deleted or transferred to other package paths, and its interfaces (including names, labels, and so on) are also likely to change, (according to Sun's point of view, we should be able to improve the performance of the Java platform by modifying the sun.* package. In this case, even if you want the program to run just under Sun's implementation platform, you will still be exposed to the risk of damage to your system from the new version. In short, it is unsafe to write Java programs that depend on the sun.* package, and they will become non-portable and cannot be properly supported.

Sound playback in Java applications

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.