我正在尝试制作wifi p2p视频聊天应用程序。我可以建立wifi-p2p连接,并且制作了一个简单的wifi p2p文本聊天应用程序。 如何捕获视频,发送和接收视频? 我尝试了以下代码。 当我尝试运行此命令时,它总是会给出一些异常,例如:illegalstateexception mediarecorder.start()或java.net.UnknownHostException
发件人线程:
public class SendVideoThread implements Runnable{
public void run(){
try {
if(SERVERIP!=null){
handler.post(new Runnable() {
@Override
public void run() {
}
});
serverSocket = new ServerSocket(SERVERPORT);
while(true) {
//listen for incoming clients
Socket client = serverSocket.accept();
handler.post(new Runnable(){
@Override
public void run(){
}
});
try{
final ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(client);
handler.post(new Runnable(){
@Override
public void run(){
recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.setVideoFrameRate(20);
recorder.setVideoSize(176,144);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
//recorder.setPreviewDisplay(mHolder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
}
});
} catch (Exception e) {
handler.post(new Runnable(){
@Override
public void run(){
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
@Override
public void run(){
}
});
}
} catch (Exception e){
handler.post(new Runnable() {
@Override
public void run() {
}
});
e.printStackTrace();
}
}
}
接收器线程:
public class ReceiveVideoThread implements Runnable{
@Override
public void run() {
try {
clientSocket = new Socket(Data.socket.getInetAddress(),SERVERPORT);
handler.post(new Runnable() {
@Override
public void run() {
}
});
handler.post(new Runnable() {
@Override
public void run() {
try {
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(clientSocket);
pfd.getFileDescriptor().sync();
mp.setDataSource(pfd.getFileDescriptor());
pfd.close();
mp.setDisplay(mHolder);
mp.prepareAsync();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}