android 启动一个摄像头录制视频并上传到服务器上

时间:2022-09-18 22:36:34
android(用程序启动摄像头录像并实时上传视频流到一台指定的电脑的目录中)

5 个解决方案

#1


求高手写代码。谢谢

#2


哇,这个好实用,期待高手解答.

#3


一般来说:使用MediaRecorder来录像,设置本地存储路径,MediaRecorder.setOutputFile("/sdcard/xxx");
如果要实时上传的话,可以把摄像头拍摄的视频流实时传入socket里:
Socket receiver = new Socket(“xxx.xxx.x.xxx”, 8890);
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(receiver);
MediaRecorder.setOutputFile(pfd.getFileDescriptor()); 
具体参考:http://blog.csdn.net/zblue78/article/details/6083374

#4


package cn.view;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class MyAndroidVideo extends Activity {
/** Called when the activity is first created. */
// 定义SurfaceView对象
private SurfaceView surfaceView = null;
// 定义Button对象
private Button record = null;
private Button cancel = null;
// 定义MediaRecorder对象
private MediaRecorder mediaRecorder = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载界面main.xml
setContentView(R.layout.main);
// 屏幕参数设置
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

// 通过findViewByIdeo找到surfaceView,record的ID
surfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
record = (Button) this.findViewById(R.id.record);
cancel = (Button) this.findViewById(R.id.cancel);

// 监听Record按钮
record.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
// 实例化MediaRecorder类的对象
mediaRecorder = new MediaRecorder();
// 获取sdCard目录
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();
System.out.println(sdCardDir);
File saveFile = new File(sdCardDir, System.currentTimeMillis() + ".3gp");
String path = saveFile.getAbsolutePath();
                            System.out.println(path);
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setVideoSize(176, 144);
mediaRecorder.setVideoFrameRate(3);

mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(path);
    mediaRecorder.setMaxDuration(10000); 


try {
Toast.makeText(MyAndroidVideo.this,"Recording......", Toast.LENGTH_LONG).show();
mediaRecorder.prepare();
mediaRecorder.start();
Toast.makeText(MyAndroidVideo.this,"Recording......", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
cancel.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Toast.makeText(MyAndroidVideo.this, "Cancelling......",
Toast.LENGTH_LONG).show();
MyAndroidVideo.this.finish();
mediaRecorder.stop();
mediaRecorder.release();
Toast.makeText(MyAndroidVideo.this, "Cancelling......",
Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});
}
}

#5


怎么能实时的传到服务器上,服务器代码怎么编写,求完善代码。

#1


求高手写代码。谢谢

#2


哇,这个好实用,期待高手解答.

#3


一般来说:使用MediaRecorder来录像,设置本地存储路径,MediaRecorder.setOutputFile("/sdcard/xxx");
如果要实时上传的话,可以把摄像头拍摄的视频流实时传入socket里:
Socket receiver = new Socket(“xxx.xxx.x.xxx”, 8890);
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(receiver);
MediaRecorder.setOutputFile(pfd.getFileDescriptor()); 
具体参考:http://blog.csdn.net/zblue78/article/details/6083374

#4


package cn.view;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class MyAndroidVideo extends Activity {
/** Called when the activity is first created. */
// 定义SurfaceView对象
private SurfaceView surfaceView = null;
// 定义Button对象
private Button record = null;
private Button cancel = null;
// 定义MediaRecorder对象
private MediaRecorder mediaRecorder = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载界面main.xml
setContentView(R.layout.main);
// 屏幕参数设置
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

// 通过findViewByIdeo找到surfaceView,record的ID
surfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
record = (Button) this.findViewById(R.id.record);
cancel = (Button) this.findViewById(R.id.cancel);

// 监听Record按钮
record.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
// 实例化MediaRecorder类的对象
mediaRecorder = new MediaRecorder();
// 获取sdCard目录
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();
System.out.println(sdCardDir);
File saveFile = new File(sdCardDir, System.currentTimeMillis() + ".3gp");
String path = saveFile.getAbsolutePath();
                            System.out.println(path);
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setVideoSize(176, 144);
mediaRecorder.setVideoFrameRate(3);

mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(path);
    mediaRecorder.setMaxDuration(10000); 


try {
Toast.makeText(MyAndroidVideo.this,"Recording......", Toast.LENGTH_LONG).show();
mediaRecorder.prepare();
mediaRecorder.start();
Toast.makeText(MyAndroidVideo.this,"Recording......", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
cancel.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Toast.makeText(MyAndroidVideo.this, "Cancelling......",
Toast.LENGTH_LONG).show();
MyAndroidVideo.this.finish();
mediaRecorder.stop();
mediaRecorder.release();
Toast.makeText(MyAndroidVideo.this, "Cancelling......",
Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});
}
}

#5


怎么能实时的传到服务器上,服务器代码怎么编写,求完善代码。