Java playback Sound class and a simple example _java

Source: Internet
Author: User
Tags cas


Classes that play sounds

Copy Code code as follows:

Import Java.io.File;
Import java.io.IOException;

Import Javax.sound.sampled.AudioFormat;
Import Javax.sound.sampled.AudioInputStream;
Import Javax.sound.sampled.AudioSystem;
Import Javax.sound.sampled.DataLine;
Import Javax.sound.sampled.SourceDataLine;

Classes that play sounds
public class Playsounds extends Thread {

private String filename;
Public Playsounds (String wavfile) {

filename = System.getproperty ("User.dir") +wavfile;
}
public void Run () {

File Soundfile = new file (filename);

Audioinputstream audioinputstream = null;
try {
Audioinputstream = Audiosystem.getaudioinputstream (soundfile);
catch (Exception E1) {
E1.printstacktrace ();
Return
}
Audioformat format = Audioinputstream.getformat ();
Sourcedataline auline = null;
Dataline.info Info = new Dataline.info (sourcedataline.class, format);
try {
Auline = (sourcedataline) audiosystem.getline (info);
Auline.open (format);
catch (Exception e) {
E.printstacktrace ();
Return
}
Auline.start ();
int nbytesread = 0;
It's a buffer.
byte[] Abdata = new byte[512];
try {
while (Nbytesread!=-1) {
Nbytesread = Audioinputstream.read (abdata, 0, abdata.length);
if (nbytesread >= 0)
Auline.write (abdata, 0, Nbytesread);
}
catch (IOException e) {
E.printstacktrace ();
Return
finally {
Auline.drain ();
Auline.close ();
}
}
}


The following is a Java application that plays sound, which can play the sound one time and loop the sound

Musicpaly mymusicplay = new Musicplay (GetClass (). GetResource ("/music/button.wav"));
Mymusicplay.start ()//play Once
Mymusicplay. Stop ();/stop
Mymusicplay. Continuousstart ()//loop playback
Mymusicplay. Continuousstop ()/Stop

Copy Code code as follows:

FileName: Muiscplay.java
Import java.io.*;
Import Java.net.URL;
Import sun.audio.*;

/**
*
* @author Wuhuiwen
* Play audio files to produce sound effects
*/
public class Musicplay {
Private audiostream as; Play a sound with a single time
Continuousaudiodatastream cas;//Loop Play sound
Constructors
Public musicplay (URL url)
{
try {
Open a sound file stream as input
as = new Audiostream (Url.openstream ());
catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Play Start once
public void Start ()
{
if (as==null) {
System.out.println ("Audiostream object is not created!");
Return
}else{
AudioPlayer.player.start (AS);
}
}
Play Stop once
public void Stop ()
{
if (as==null) {
System.out.println ("Audiostream object is not created!");
Return
}else{
AudioPlayer.player.stop (AS);
}
}
Loop Play Start
public void Continuousstart ()
{
Create Audiodata Source.
Audiodata data = null;
try {
data = As.getdata ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Create Continuousaudiodatastream.
cas = new Continuousaudiodatastream (data);

Play audio.
AudioPlayer.player.start (CAS);
}
Loop playback Stop
public void Continuousstop ()
{
if (CAS!= null)
{
AudioPlayer.player.stop (CAS);
}
}

}

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.