标签:
AudioSystem
类包括许多操作 AudioInputStream
对象的方法:
getAudioInputStream(File file)
public class audioInputStream { public static void playWAV(){ try { AudioInputStream stream = AudioSystem.getAudioInputStream(new File("SourceFile/1.wav")); byte[] samples = getSamples(stream); //将音频转化为字节数组 InputStream in = new ByteArrayInputStream(samples); play(in,stream.getFormat()); //播放音频文件 } catch (UnsupportedAudioFileException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static byte[] getSamples(AudioInputStream stream){ int length = (int) (stream.getFrameLength()*stream.getFormat().getFrameSize()); byte[] samples = new byte[length]; DataInputStream in = new DataInputStream(stream); try { in.readFully(samples); System.out.println(length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return samples; } private static void play(InputStream stream, AudioFormat format){ int bufferSize = format.getFrameSize()* Math.round(format.getSampleRate()/10); byte[] buffer = new byte[bufferSize]; DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try { SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, bufferSize); line.start(); int numBytesRead = 0; while(numBytesRead != -1){ numBytesRead = stream.read(buffer, 0, buffer.length); if(numBytesRead != -1){ line.write(buffer, 0, numBytesRead); //System.out.println(numBytesRead); } } line.drain(); line.close(); } catch (LineUnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
try { FileInputStream fis = new FileInputStream("SourceFile/employee"); try { byte[] bytes = new byte[fis.available()]; fis.read(bytes); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
//System.in是一个位流,为了转换为字符流,可使用InputStreamReader为其进行字符转换, //然后再使用BufferedReader为其增加缓冲功能。 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String content = null; try { while(!(content = br.readLine()).equals("quit")){ System.out.println(content); } br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
StringReader sr = new StringReader("dsfasdfasdfasd"); char[] chars = new char[5]; //每次读取5个字符 int length = 0; try { while((length = sr.read(chars)) != -1){ String strRead = new String(chars, 0, length).toUpperCase(); System.out.println(strRead); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
标签:
原文地址:http://www.cnblogs.com/zhanglei93/p/5855217.html