java播放声音文件

支持.au声音文件,其它的格式没试过。

package com.mytest;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
 

public class Audio {
	InputStream in = null;
	ContinuousAudioDataStream cas = null;

	public void start() // 这是通过流读取声音文件
	{
		try {
			in = new FileInputStream("D:/2.au");

			AudioStream as = new AudioStream(in);

			//下面两行是持续响起,注掉后则只响一次。----start
			AudioData data = as.getData();

			ContinuousAudioDataStream cas = new ContinuousAudioDataStream(data);
			//----end
			AudioPlayer.player.start(cas);

		} catch (IOException e) {

			System.out.println(e.getMessage());

		}
	}

	public void close() throws Exception {

		AudioPlayer.player.stop(cas);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Audio ad = new Audio();

		ad.start();

	}

}

你可能感兴趣的:(java,sun)