我是靠谱客的博主 轻松耳机,最近开发中收集的这篇文章主要介绍socket播放音频,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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


转载于:https://blog.51cto.com/5232047/1333199

最后

以上就是轻松耳机为你收集整理的socket播放音频的全部内容,希望文章能够帮你解决socket播放音频所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部