zoukankan      html  css  js  c++  java
  • java音乐播放之IO流处理

    这个类仅仅能一直播放。知道音乐结束。

    比AudioCilp要好一点。

    import java.io.*;
    
    import javax.sound.sampled.*;
    
    public class Test {
    	public static void main(String[] args) {
    		AePlayWave apw=new AePlayWave("./111.wav");
    		apw.start();
    	}
    
    }
    
    //播放声音的类
    class AePlayWave extends Thread {
    
    	private String filename;
    	public AePlayWave(String wavfile) {
    		filename = 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();
    		}
    
    	}
    
    	
    }
    










  • 相关阅读:
    IDOC练习(一、发送端配置)
    节约性能的一个sql
    获取用户IP地址及用户名
    获取一个结构的所有字段
    获取文件名称的两个函数
    时间戳
    SMOD和CMOD
    内表生成XML简单实例
    ABAP 编辑器设置(主动…
    ABAP常用Function
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7044743.html
Copyright © 2011-2022 走看看