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


播放声音的类

复制代码 代码如下:

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);
        }   
    }

}

你可能感兴趣的:(java播放声音类和一个简单示例)