点对点视频流
我正在尝试开发一个点对点应用程序,它将在客户端之间传输实时视频。客户将加入并随后能够进行流式传输。
下面的代码不会崩溃,但不允许流式传输。请帮助我,因为我想知道问题是什么。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.Buffer;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.VideoView;
public class StreamingclienttestActivity extends Activity {
/** Called when the activity is first created. */
StreamingMediaPlayer smp;
boolean paused = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Add Activity as callback for SurfaceView
final VideoView vv = (VideoView) findViewById(R.id.videoView1);
try {
//The IP of the streaming server and the port number
Socket sock = new Socket("10.68.50.63",3333);
//Get the input stream from the socket
final InputStream is = sock.getInputStream();
if (is == null)
throw new RuntimeException("stream is null");
final File temp = File.createTempFile("mediaplayertmp", "dat",new File("/sdcard/"));
String tempPath = temp.getAbsolutePath();
final FileOutputStream out = null;
new Thread(new Runnable() {
public void run() {
byte buf[] = new byte[1024];
do {
int numread = -1;
try {
numread = is.read(buf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (numread <= 0)
break;
try {
out.write(buf, 0, numread);
out.equals(temp) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (true);
//Start the StreamingMediaPlayer with the input stream
smp = new StreamingMediaPlayer("temp",vv);
//Trigger playback
smp.play();
}
}
).start();
}
catch (Exception e) {
Log.w("StreamingcliettestActivity", "Exception while instantiating smp.");
e.printStackTrace();
}
new Thread(new Runnable() {
public void run() {
try {
ServerSocket ssock = new ServerSocket(3333);
//Log.w(LOGTAG, "Waiting for connection");
Socket s = ssock.accept();
BufferedOutputStream outStream = new BufferedOutputStream(s.getOutputStream());
FileInputStream fileInput = new FileInputStream(new File("sdcard/temp"));
//Log.w(LOGTAG, "Start sending data");
byte[] buff = new byte[1024];
int read = -1;
while ((read = fileInput.read(buff)) != -1) {
outStream.write(buff, 0, read);
}
outStream.flush();
outStream.close();
//Log.w(LOGTAG, "Closed connection");
} catch (Exception e) {
Log.w("StreamingcliettestActivity", "Exception while instantiating smp.");
e.printStackTrace();
}
}
}).start();
}
// public class StreamingclientSERVERtestActivity extends StreamingclienttestActivity {
// private static final String LOGTAG = "StreamingClientServer";
// public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
@Override
public void onConfigurationChanged(Configuration newCfg) {
//ignore orientation change
super.onConfigurationChanged(newCfg);
}
@Override
public void onDestroy() {
smp.close();
super.onDestroy();
}
}
正如所写,我正在尝试打开一个从输入流读取的套接字。我试图将此输入流写入一个文件(该文件被创建为 0 字节!!),但其中没有任何内容。我正在尝试使用这个现有的流将这部分传输给另一个客户端。
请帮忙
I am trying to develop a peer to peer application which will stream live video between the clients. The clients will be joining in and will then be able to stream.
The below code does not crash but it does not allow the streaming. Please help me as i would like to know what is the problem.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.Buffer;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.VideoView;
public class StreamingclienttestActivity extends Activity {
/** Called when the activity is first created. */
StreamingMediaPlayer smp;
boolean paused = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Add Activity as callback for SurfaceView
final VideoView vv = (VideoView) findViewById(R.id.videoView1);
try {
//The IP of the streaming server and the port number
Socket sock = new Socket("10.68.50.63",3333);
//Get the input stream from the socket
final InputStream is = sock.getInputStream();
if (is == null)
throw new RuntimeException("stream is null");
final File temp = File.createTempFile("mediaplayertmp", "dat",new File("/sdcard/"));
String tempPath = temp.getAbsolutePath();
final FileOutputStream out = null;
new Thread(new Runnable() {
public void run() {
byte buf[] = new byte[1024];
do {
int numread = -1;
try {
numread = is.read(buf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (numread <= 0)
break;
try {
out.write(buf, 0, numread);
out.equals(temp) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (true);
//Start the StreamingMediaPlayer with the input stream
smp = new StreamingMediaPlayer("temp",vv);
//Trigger playback
smp.play();
}
}
).start();
}
catch (Exception e) {
Log.w("StreamingcliettestActivity", "Exception while instantiating smp.");
e.printStackTrace();
}
new Thread(new Runnable() {
public void run() {
try {
ServerSocket ssock = new ServerSocket(3333);
//Log.w(LOGTAG, "Waiting for connection");
Socket s = ssock.accept();
BufferedOutputStream outStream = new BufferedOutputStream(s.getOutputStream());
FileInputStream fileInput = new FileInputStream(new File("sdcard/temp"));
//Log.w(LOGTAG, "Start sending data");
byte[] buff = new byte[1024];
int read = -1;
while ((read = fileInput.read(buff)) != -1) {
outStream.write(buff, 0, read);
}
outStream.flush();
outStream.close();
//Log.w(LOGTAG, "Closed connection");
} catch (Exception e) {
Log.w("StreamingcliettestActivity", "Exception while instantiating smp.");
e.printStackTrace();
}
}
}).start();
}
// public class StreamingclientSERVERtestActivity extends StreamingclienttestActivity {
// private static final String LOGTAG = "StreamingClientServer";
// public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
@Override
public void onConfigurationChanged(Configuration newCfg) {
//ignore orientation change
super.onConfigurationChanged(newCfg);
}
@Override
public void onDestroy() {
smp.close();
super.onDestroy();
}
}
As written, i am trying to open up a socket which reads from the input stream. I am trying to write this input stream to a file (which is being created as 0 bytes!!) but nothing in it. I am trying t use this existing stream this part to another client.
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论