最近要做一个项目,android客户端 socket实时传输视频到服务器。一点头绪都没有。高人们指点下,最好给代码
答案:2 悬赏:40 手机版
解决时间 2022-01-01 06:21
- 提问者网友:蔚蓝的太阳
- 2021-12-31 22:50
没分了,不好意思。麻烦大牛们帮帮忙
最佳答案
- 五星知识达人网友:酒者煙囻
- 2021-12-31 23:26
额··视频没传过··不知道音频可不可以··我做过安卓基于wifi的对讲机··用的是UDPsocket传输的···
package com.interphone;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class InterphoneActivity extends Activity {
private Button startBTN, stopBTN, exitBTN;
private EditText hostIP, goalIP;
private String hostIPAddrest;
private String goalIPAddrest;
private boolean start = true;
private DatagramSocket udpSocket;
private static final int DEFAULT_PORT = 43708;
private static final int frequency = 8000;
private static final int channalConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int recBufSize, playBufSize;
AudioRecord audioRecord;
AudioTrack audioTrack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// buffer 申请
recBufSize = AudioRecord.getMinBufferSize(frequency,
channalConfiguration, audioEncoding);
playBufSize = android.media.AudioTrack.getMinBufferSize(frequency,
channalConfiguration, audioEncoding);
// ------------------------------------------------------------
// audioRecord 申明
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channalConfiguration, audioEncoding, recBufSize);
// audioTrack 申明
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
channalConfiguration, audioEncoding, playBufSize,
AudioTrack.MODE_STREAM);
// ------------------------------------------------------------
findView();
getHostIP();
onClickListener();
}
private void onClickListener() {
// TODO Auto-generated method stub
startBTN.setOnClickListener(listener);
stopBTN.setOnClickListener(listener);
exitBTN.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == startBTN) {
goalIPAddrest = getGoalIP();
if (goalIPAddrest == null) {
Toast.makeText(InterphoneActivity.this, "目标IP为空",
Toast.LENGTH_SHORT).show();
} else {
start = true;
try {
udpSocket = new DatagramSocket(DEFAULT_PORT);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new RecordThread().start();
new PlayThread().start();
}
} else if (v == stopBTN) {
start = false;
} else if (v == exitBTN) {
start = false;
InterphoneActivity.this.finish();
}
}
};
class PlayThread extends Thread {
public void run() {
DatagramPacket dataPacket = null;
audioTrack.play();
byte[] buffer = new byte[recBufSize];
dataPacket = new DatagramPacket(buffer, recBufSize);
while (start) {
try {
udpSocket.receive(dataPacket);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
if (dataPacket.getLength() != 0) {
audioTrack.write(buffer, 0, buffer.length);
}
}
audioTrack.stop();
}
}
class RecordThread extends Thread {
public void run() {
DatagramPacket dataPacket = null;
try {
byte[] buffer = new byte[recBufSize];
audioRecord.startRecording();
dataPacket = new DatagramPacket(buffer, recBufSize);
InetAddress goalInetAddress;
goalInetAddress = InetAddress.getByName(goalIPAddrest);
dataPacket.setAddress(goalInetAddress);
while (start) {
int bufferReadResult = audioRecord.read(buffer, 0,
recBufSize);
byte[] tmpBuf = new byte[bufferReadResult];
System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
dataPacket.setData(tmpBuf);
dataPacket.setLength(tmpBuf.length);
dataPacket.setPort(DEFAULT_PORT);
udpSocket.send(dataPacket);
}
audioRecord.stop();
} catch (Exception e) {
// TODO: handle exception
}
}
}
private String getGoalIP() {
// TODO Auto-generated method stub
return goalIP.getText().toString();
}
private void getHostIP() {
// TODO Auto-generated method stub
hostIPAddrest = getLocalIPAddress();
if (hostIPAddrest != null) {
hostIP.setText(hostIPAddrest);
} else {
hostIP.setText("Can not get IP address");
Toast.makeText(InterphoneActivity.this, "可能未连接WIFI",
Toast.LENGTH_SHORT).show();
}
}
private String getLocalIPAddress() {
// TODO Auto-generated method stub
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("getIP", ex.toString());
}
return null;
}
private void findView() {
// TODO Auto-generated method stub
startBTN = (Button) findViewById(R.id.startBTN);
stopBTN = (Button) findViewById(R.id.stopBTN);
exitBTN = (Button) findViewById(R.id.exitBTN);
hostIP = (EditText) findViewById(R.id.hostIP);
goalIP = (EditText) findViewById(R.id.goalIP);
}
}
代码··写的太乱了··自己都不想看了··你看下吧··希望对你有帮助吧···
package com.interphone;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class InterphoneActivity extends Activity {
private Button startBTN, stopBTN, exitBTN;
private EditText hostIP, goalIP;
private String hostIPAddrest;
private String goalIPAddrest;
private boolean start = true;
private DatagramSocket udpSocket;
private static final int DEFAULT_PORT = 43708;
private static final int frequency = 8000;
private static final int channalConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int recBufSize, playBufSize;
AudioRecord audioRecord;
AudioTrack audioTrack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// buffer 申请
recBufSize = AudioRecord.getMinBufferSize(frequency,
channalConfiguration, audioEncoding);
playBufSize = android.media.AudioTrack.getMinBufferSize(frequency,
channalConfiguration, audioEncoding);
// ------------------------------------------------------------
// audioRecord 申明
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channalConfiguration, audioEncoding, recBufSize);
// audioTrack 申明
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
channalConfiguration, audioEncoding, playBufSize,
AudioTrack.MODE_STREAM);
// ------------------------------------------------------------
findView();
getHostIP();
onClickListener();
}
private void onClickListener() {
// TODO Auto-generated method stub
startBTN.setOnClickListener(listener);
stopBTN.setOnClickListener(listener);
exitBTN.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == startBTN) {
goalIPAddrest = getGoalIP();
if (goalIPAddrest == null) {
Toast.makeText(InterphoneActivity.this, "目标IP为空",
Toast.LENGTH_SHORT).show();
} else {
start = true;
try {
udpSocket = new DatagramSocket(DEFAULT_PORT);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new RecordThread().start();
new PlayThread().start();
}
} else if (v == stopBTN) {
start = false;
} else if (v == exitBTN) {
start = false;
InterphoneActivity.this.finish();
}
}
};
class PlayThread extends Thread {
public void run() {
DatagramPacket dataPacket = null;
audioTrack.play();
byte[] buffer = new byte[recBufSize];
dataPacket = new DatagramPacket(buffer, recBufSize);
while (start) {
try {
udpSocket.receive(dataPacket);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
if (dataPacket.getLength() != 0) {
audioTrack.write(buffer, 0, buffer.length);
}
}
audioTrack.stop();
}
}
class RecordThread extends Thread {
public void run() {
DatagramPacket dataPacket = null;
try {
byte[] buffer = new byte[recBufSize];
audioRecord.startRecording();
dataPacket = new DatagramPacket(buffer, recBufSize);
InetAddress goalInetAddress;
goalInetAddress = InetAddress.getByName(goalIPAddrest);
dataPacket.setAddress(goalInetAddress);
while (start) {
int bufferReadResult = audioRecord.read(buffer, 0,
recBufSize);
byte[] tmpBuf = new byte[bufferReadResult];
System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
dataPacket.setData(tmpBuf);
dataPacket.setLength(tmpBuf.length);
dataPacket.setPort(DEFAULT_PORT);
udpSocket.send(dataPacket);
}
audioRecord.stop();
} catch (Exception e) {
// TODO: handle exception
}
}
}
private String getGoalIP() {
// TODO Auto-generated method stub
return goalIP.getText().toString();
}
private void getHostIP() {
// TODO Auto-generated method stub
hostIPAddrest = getLocalIPAddress();
if (hostIPAddrest != null) {
hostIP.setText(hostIPAddrest);
} else {
hostIP.setText("Can not get IP address");
Toast.makeText(InterphoneActivity.this, "可能未连接WIFI",
Toast.LENGTH_SHORT).show();
}
}
private String getLocalIPAddress() {
// TODO Auto-generated method stub
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("getIP", ex.toString());
}
return null;
}
private void findView() {
// TODO Auto-generated method stub
startBTN = (Button) findViewById(R.id.startBTN);
stopBTN = (Button) findViewById(R.id.stopBTN);
exitBTN = (Button) findViewById(R.id.exitBTN);
hostIP = (EditText) findViewById(R.id.hostIP);
goalIP = (EditText) findViewById(R.id.goalIP);
}
}
代码··写的太乱了··自己都不想看了··你看下吧··希望对你有帮助吧···
全部回答
- 1楼网友:末日狂欢
- 2022-01-01 00:54
1. pc服务器启用serversocket
两个通信实体在建立虚拟链路之前,需要有一方先准备好,主动接受来自其他通信实体的连接请求。
使用serversocket对象监听来自客户端的socket连接。
//创建serversocket对象
serversocket ss = new serversocket(30000);
//监听来自客户端的请求
while(true){
socket s = ss.accept();
…
}
如果没有连接,则将一直处于等待状态。当接收到连接请求后,获取消息到输入流,并保存到文件。
//接收客户端消息
bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream()));
string str;
bufferedwriter bw = new bufferedwriter(new filewriter("d:/apinfo"+ (i++) +".txt"));
while ((str = in.readline()) != null) {
system.out.println(str);
bw.write(str);
bw.newline();
}
2. android终端使用socket通信
客户端使用socket的构造器连接服务器,指定服务器ip和端口号就可以了。
socket s = new socket(“192.168.1.100”, 30000);
这样服务器端的accept()方法就得到响应,从而向下执行,服务器端和客户端就形成了一对互相连接的socket。再进行通信时就没有服务器和客户端之分了,都是通过输入输出流进行通信。
详细步骤:
采用handler和timertask来定时扫描ap信息并发送给服务器端。timertask规定了到达指定的时间所要进行的任务。
timertask task = new timertask(){
public void run() {
message message = new message();
message.what = 1;
handler.sendmessage(message);
}
};
handler传递message内容:
handler handler = new handler(){
public void handlemessage(message msg) {
switch (msg.what) {
case 1:
// 执行定时器时间到了之后由handler传递的任务
break;
}
super.handlemessage(msg);
}
}
接下来扫描ap信息并发送给服务器端,然后将结果保存。
wifimanager wifimanager=(wifimanager) getsystemservice(wifi_service);
wifimanager.startscan();
mwifilist = wifimanager.getscanresults();
由wifimanager说明可知,它可以用于处理已配置的网络,当前连接的网络及ap信息的扫描等情况。
向服务器发送消息:
socket = new socket("192.168.1.211",30000);
//向服务器端发送消息
printwriter out = new printwriter( new bufferedwriter( new outputstreamwriter(socket.getoutputstream())),true);
out.println(message);
其中message为获取的ap信息,测试收到的信息格式为:
ssid: icis_lab, bssid: 1c:af:f7:9a:65:e4, capabilities: [wpa-psk-tkip+ccmp], level: -80,frequency: 2437
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯