欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java播放声音类和一个简单示例

程序员文章站 2024-02-23 20:44:34
播放声音的类 复制代码 代码如下:import java.io.file;import java.io.ioexception; import javax.sound....


播放声音的类

复制代码 代码如下:

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;

//播放声音的类
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;
  //这是缓冲
  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();
  }
 } 
}


下面是一个java播放声音的应用程序,可以单次播放声音、循环播放声音

musicpaly mymusicplay = new musicplay(getclass().getresource("/music/button.wav"));
mymusicplay.start();//播放一次
mymusicplay .stop();//停止
mymusicplay .continuousstart();//循环播放
mymusicplay .continuousstop();//停止

复制代码 代码如下:

// 文件名:muiscplay.java
import java.io.*;
import java.net.url;
import sun.audio.*;

/**
*
* @author wuhuiwen
* 播放音频文件,产生音效
*/
public class musicplay {
    private audiostream  as; //单次播放声音用
    continuousaudiodatastream cas;//循环播放声音
    // 构造函数
    public musicplay(url url)
    {
        try {
            //打开一个声音文件流作为输入
            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();
        }
    }
    // 一次播放 开始
    public void start()
    {
        if( as==null ){
            system.out.println("audiostream object is not created!");
            return;
        }else{
            audioplayer.player.start (as);
        }
    }
    // 一次播放 停止
    public void stop()
    {
        if( as==null ){
            system.out.println("audiostream object is not created!");
            return;
        }else{
            audioplayer.player.stop(as);
        }       
    }
    // 循环播放 开始
    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);
    }
    // 循环播放 停止
    public void continuousstop()
    {
        if(cas != null)
        {
            audioplayer.player.stop (cas);
        }   
    }

}