package client; //import java.io.BufferedReader; //import java.io.ByteArrayInputStream; //import java.io.DataInputStream; //import java.io.DataOutputStream; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; //import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; public class Client { public static void main(String[] args) throws Exception, Exception { // 链接sever端 Socket s1 = new Socket("192.168.137.10", 8080); System.out.println("连接成功"); // 实例化输入流 //DataInputStream dis = new DataInputStream(s1.getInputStream()); InputStream dis = s1.getInputStream(); // 实例化输出流 //DataOutputStream dos = new DataOutputStream(s1.getOutputStream()); OutputStream dos = s1.getOutputStream(); // 实例化两个进程 // Thread mcr = new ClientRead(dis); // 音频接受线程 Thread mcw = new ClientWrite(dos); new Playback(s1).start(); new SaveFile(dis).start(); System.out.println("线程创建成功"); // 启动两个进程 // mcr.start(); mcw.start(); } } // 创建一个线程用来进行接收读取数据 class ClientRead extends Thread { private InputStream dis; public ClientRead(InputStream dis) { this.dis = dis; } public void run() { // String msg; byte[] voice = new byte[80000]; System.out.println("数组创建成功"); try { while (dis.read(voice) < 80000){ System.out.println("收到数据"); } System.out.println("开始输出"); for(int count=0;count<=70;count++) { System.out.println((byte)voice[count]); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } // 创建一个进程用来写入并发送数据 class ClientWrite extends Thread { private OutputStream dos; public ClientWrite(OutputStream dos) { this.dos = dos; } @Override public void run() { //BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); byte[] instruction=new byte[2]; instruction[0]=(byte)0x67; instruction[1]=(byte)0x82; try { // System.in.read(instruction); // System.out.println(instruction); dos.write(instruction); System.out.println("发送成功"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //InputStream start = new ByteArrayInputStream(instruction); // String msg; } } /** * Title: VoiceChat * Description: 输出音频(放音程序) * Copyright: Copyright (c) 2001 * Company: * @author * @version 1.0 * 步骤: * 首先读取网络数据流到byte,新建line音频管线,用line的write方法将视频流输出到混频器 * 目前存在问题:1.噪音较大。2.声音不连贯,时断时续 * */ class Playback implements Runnable { final int bufSize = 16384; SourceDataLine line; Thread thread; Socket s; Playback(Socket s){//构造器 取得socket以获得网络输入流 this.s=s; } public void start() { thread = new Thread(this); thread.setName("Playback"); thread.start(); } public void stop() { thread = null; } public void run() { // AudioFormat format =new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,8000.0f,8,1,1,8000.0f,false);//AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian) AudioFormat format =new AudioFormat(8000.0f,8,1,true,false); BufferedInputStream playbackInputStream; //获取音频流 try { // ByteArrayInputStream bin=new ByteArrayInputStream(null); // bin.read playbackInputStream=new BufferedInputStream(new AudioInputStream(s.getInputStream(),format,800000000));//封装成音频输出流 } catch (IOException ex) { return; } DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);//line音频管道信息 try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, bufSize); //打开管道 } catch (LineUnavailableException ex) { return; } byte[] data = new byte[3752];// int numBytesRead = 0; line.start(); while (thread != null) { try{ numBytesRead = playbackInputStream.read(data); int count=line.write(data, 0,numBytesRead); System.out.println(count); } catch (IOException e) { break; } } if (thread != null) { line.drain(); } line.stop(); line.close(); line = null; } }
socket播放音频
原创wutongkege ©著作权
©著作权归作者所有:来自51CTO博客作者wutongkege的原创作品,请联系作者获取转载授权,否则将追究法律责任
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
播放音频
使用mediaplayer播放音频文件 [1]作用:这个类用于播放音频或者是视频
播放音频 ide 初始化 bundle 数据