两部 Android 手机之间的实时视频流
我目前正在研究两部 Android 手机之间的视频流。 我编写了一个能够将视频录制到 sd 文件的应用程序(使用 MediaRecorder);我编写了另一个能够显示文件视频的应用程序。两个应用程序都可以完美运行。
我在以下网站中找到了一个关于“使用 Android 广播视频 - 无需写入本地文件”的网站。这正是我想做的。
我修改了我的代码。
对于录像机来说, 它是:
socket=severSocket.accept();
ParcelFileDescriptor=pfd;
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setVideoFrameRate(15);
recorder.setVideoSize(320, 240);
recorder.setPreviewDisplay(holder.getSurface());
pfd = ParcelFileDescriptor.fromSocket(socket);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.prepare();
recorder.start();
对于视频播放器:
Socket socket = new Socket(IP,PORT);
mMediaPlayer = new MediaPlayer();
pfd = ParcelFileDescriptor.fromSocket(socket);
mMediaPlayer.setDataSource(pfd.getFileDescriptor()); // <-- here is the problem
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.start();
在 MediaPlayer 上对 mMediaPlayer.setDataSource(pfd.getFileDescriptor());
进行程序粉碎 我知道我没有正确设置数据源。必须对 ParcelFileDescriptor 进行额外设置才能放入 MediaPlayer。
有谁知道如何使用 MediaPlayer 的 ParcelFileDescriptor ? 任何有用的建议或提示都会很好......
谢谢
你
I am currently working on video streaming between two Android Phone.
I wrote an application which is able to record the video to the sd file (Using MediaRecorder); and I wrote another application which is able to display the video of the file. Both applications work perfectly.
I found a website about "Broadcasting video with Android - without writing to local files" in following website. It is exactly what I wanted to do.
I modified my code.
For the video recorder,
it is:
socket=severSocket.accept();
ParcelFileDescriptor=pfd;
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setVideoFrameRate(15);
recorder.setVideoSize(320, 240);
recorder.setPreviewDisplay(holder.getSurface());
pfd = ParcelFileDescriptor.fromSocket(socket);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.prepare();
recorder.start();
For Video Player:
Socket socket = new Socket(IP,PORT);
mMediaPlayer = new MediaPlayer();
pfd = ParcelFileDescriptor.fromSocket(socket);
mMediaPlayer.setDataSource(pfd.getFileDescriptor()); // <-- here is the problem
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.start();
Program crush on mMediaPlayer.setDataSource(pfd.getFileDescriptor());
on MediaPlayer
I know I didnt setup the DataSource correctly. There must be additional setups for ParcelFileDescriptor to put into MediaPlayer.
Does anyone know how to use ParcelFileDescriptor for MediaPlayer?
Any helpful advise or tips would be nice......
Thank You
Will
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在视频播放端
您必须创建一个欢迎套接字
并使用
而不是
in the video playing side
you must create a welcome socket
and use
instead of
Android 本身不支持 Android 2.1 或更低版本的视频流。我们所做的就是逐帧获取图像;并将每个火焰分解为 BYTE[] 并使用 Socket 类发送。在接收方,我们使用接收到的
BYTE[]
数据重建图像。Android does not natively support video streaming in Android 2.1 or below. What we did was to get the images frame by frame; and break each flame into BYTE[] and send over using
Socket
class. And in receiver's side, we rebuild the images usingBYTE[]
data received.