手机录制视频demo

复制代码
public class VideoActivity extends Activity {

    
    private File myRecAudioFile;
    private SurfaceView mSurfaceView;   
    private SurfaceHolder mSurfaceHolder; 
    private Button buttonStart;
    private Button buttonStop;
    private File dir;
    private MediaRecorder recorder;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video);
        mSurfaceView = (SurfaceView) findViewById(R.id.videoView);   
  
        mSurfaceHolder = mSurfaceView.getHolder();   
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        buttonStart=(Button)findViewById(R.id.start);
        buttonStop=(Button)findViewById(R.id.stop);

        
        
        
         dir = Environment.getExternalStorageDirectory();
         recorder = new MediaRecorder();
        buttonStart.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                recorder();
                
            }
        });
        buttonStop.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 recorder.stop();
                 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
                 recorder.release();
                 recorder=null;
                 
                 dialog();
            }
        });
    }
    public void recorder() {
        try {
//             dir = Environment.getExternalStorageDirectory();
//            MediaRecorder recorder = new MediaRecorder();
            myRecAudioFile = File.createTempFile("video", ".3gp",
                    dir);
            //myRecAudioFile = File.createTempFile("video", ".mp4",
                //    dir);
            
            recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            // 设置录音源为麦克风
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            // 设置输出格式为3gp
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            // 设置视频大小
            recorder.setVideoSize(176, 144);
            //recorder.setVideoSize(400, 350);
            recorder.setVideoFrameRate(15);
            // 设置视频编码
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
            // 设置音频编码
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setMaxDuration(10000);
            recorder.setOutputFile(myRecAudioFile.getAbsolutePath());
            recorder.prepare();
            recorder.start();
            
            
            
//            WindowManager wm = (WindowManager) getSystemService(VideoActivity.this.WINDOW_SERVICE);//获取窗口服务
//            Display display = wm.getDefaultDisplay();//获取屏幕信息
//            recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
//            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频
//            recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
//            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//            recorder.setVideoSize(display.getWidth(), display.getHeight()); //大小为屏幕的宽和高
//            recorder.setVideoFrameRate(3); //每秒3帧
//            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式
//            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//            recorder.setOutputFile(myRecAudioFile.getAbsolutePath());
//             recorder.prepare();//预期准备
//             recorder.start();   
            
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 public void dialog(){
     
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setMessage("是否立刻上传录制的视频?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    ContentDialog.LoginDialog(VideoActivity.this).show();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
     AlertDialog alert = builder.create();
     alert.show();
 }
}
复制代码
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">  
<SurfaceView android:id="@+id/videoView"
        android:visibility="visible" android:layout_width="400px" android:layout_height="210px">
</SurfaceView>
<RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">  
<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="录制"
  android:id="@+id/start">  
></Button>
<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/start"
  android:text="停止"
  android:id="@+id/stop">  
></Button>
</RelativeLayout>
</LinearLayout>
复制代码

 

posted on   vus520  阅读(486)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2011年8月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示