zoukankan      html  css  js  c++  java
  • [zz http://www.cnblogs.com/oldfeel/archive/2012/05/15/2501290.html] android 录像/打开video文件

    先建立录像窗口 map_video.xml

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <SurfaceView
            android:id="@+id/arc_hf_video_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
        <TextView
            android:id="@+id/arc_hf_video_timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="40dp"
            android:textColor="#ffff0000"
            android:textSize="35dp"
            android:textStyle="bold" >
        </TextView>
    
        <LinearLayout
            android:id="@+id/arc_hf_video_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:orientation="vertical" >
    
            <Button
                android:id="@+id/arc_hf_video_start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/arc_hf_video_start" />
    
            <Button
                android:id="@+id/arc_hf_video_return"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/arc_hf_video_return" />
        </LinearLayout>
    
    </RelativeLayout>
    复制代码

    主类 ArcHFVideo.java

    复制代码
    public class ArcHFVideo extends Activity implements SurfaceHolder.Callback {
        private Button mVideoStartBtn, mVideoReturnBtn;
        private SurfaceView mSurfaceview;
        private MediaRecorder mMediaRecorder;
        private SurfaceHolder mSurfaceHolder; //
        private File mRecVedioPath;
        private File mRecAudioFile;
        private TextView timer;
        private int hour = 0;
        private int minute = 0;
        private int second = 0;
        private boolean bool;
        private int parentId;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
            setContentView(R.layout.map_video);
    
            parentId = getIntent().getIntExtra("parentId", 0);
            timer = (TextView) findViewById(R.id.arc_hf_video_timer);
            mVideoStartBtn = (Button) findViewById(R.id.arc_hf_video_start);
            mVideoReturnBtn = (Button) findViewById(R.id.arc_hf_video_return);
            mSurfaceview = (SurfaceView) this.findViewById(R.id.arc_hf_video_view);
    
            // 设置计时器不可见
            timer.setVisibility(View.GONE);
    
            // 设置缓存路径
            mRecVedioPath = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/hfdatabase/temp/");
            if (!mRecVedioPath.exists()) {
                mRecVedioPath.mkdirs();
            }
    
            // 绑定预览视图
            SurfaceHolder holder = mSurfaceview.getHolder();
            holder.addCallback(ArcHFVideo.this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
            mVideoStartBtn.setOnClickListener(new Button.OnClickListener() {
                private boolean isRecording = true;
    
                @Override
                public void onClick(View v) {
                    Drawable iconStart = getResources().getDrawable(
                            R.drawable.arc_hf_video_start);
                    Drawable iconStop = getResources().getDrawable(
                            R.drawable.arc_hf_video_stop);
                    if (isRecording) {
                        mVideoStartBtn.setBackgroundDrawable(iconStop);
                        bool = true;
                        if (mMediaRecorder == null)
                            mMediaRecorder = new MediaRecorder();
                        else
                            mMediaRecorder.reset();
                        mMediaRecorder.setPreviewDisplay(mSurfaceHolder
                                .getSurface());
                        mMediaRecorder
                                .setVideoSource(MediaRecorder.VideoSource.CAMERA);
                        mMediaRecorder
                                .setAudioSource(MediaRecorder.AudioSource.MIC);
                        mMediaRecorder
                                .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mMediaRecorder
                                .setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                        mMediaRecorder
                                .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mMediaRecorder.setVideoSize(176, 144);
                        mMediaRecorder.setVideoFrameRate(15);
                        try {
                            mRecAudioFile = File.createTempFile("Vedio", ".3gp",
                                    mRecVedioPath);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        mMediaRecorder.setOutputFile(mRecAudioFile
                                .getAbsolutePath());
                        try {
                            mMediaRecorder.prepare();
                            timer.setVisibility(View.VISIBLE);
                            handler.postDelayed(task, 1000);
                            mMediaRecorder.start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        isRecording = !isRecording;
                    } else {
                        mVideoStartBtn.setBackgroundDrawable(iconStart);
                        if (mMediaRecorder != null) {
                            mMediaRecorder.stop();
                            bool = false;
                            timer.setText(format(hour) + ":" + format(minute) + ":"
                                    + format(second));
                            mMediaRecorder.release();
                            mMediaRecorder = null;
                            audioRename();
                        }
                        isRecording = !isRecording;
                    }
                }
            });
            mVideoReturnBtn.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mMediaRecorder != null) {
                        mMediaRecorder.stop();
                        mMediaRecorder.release();
                        mMediaRecorder = null;
                    }
                    audioRename();
                    finish();
                }
            });
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mSurfaceHolder = holder;
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            mSurfaceHolder = holder;
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            mSurfaceview = null;
            mSurfaceHolder = null;
            mMediaRecorder = null;
        }
    
        /*
         * 生成Audio文件名字
         */
        protected void audioRename() {
            String path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath()
                    + "/hfdatabase/video/"
                    + String.valueOf(parentId) + "/";
            String fileName = new SimpleDateFormat("yyyyMMddHHmmss")
                    .format(new Date()) + ".3gp";
            File out = new File(path);
            if (!out.exists()) {
                out.mkdirs();
            }
            out = new File(path, fileName);
            mRecAudioFile.renameTo(out);
        }
    
        /*
         * 定时器设置,实现计时
         */
        private Handler handler = new Handler();
        private Runnable task = new Runnable() {
            public void run() {
                if (bool) {
                    handler.postDelayed(this, 1000);
                    second++;
                    if (second < 60) {
                        timer.setText(format(second));
                    } else if (second < 3600) {
                        minute = second / 60;
                        second = second % 60;
                        timer.setText(format(minute) + ":" + format(second));
                    } else {
                        hour = second / 3600;
                        minute = (second % 3600) / 60;
                        second = (second % 3600) % 60;
                        timer.setText(format(hour) + ":" + format(minute) + ":"
                                + format(second));
                    }
                }
            }
        };
    
        /*
         * 格式化时间
         */
        public String format(int i) {
            String s = i + "";
            if (s.length() == 1) {
                s = "0" + s;
            }
            return s;
        }
    }
    复制代码

    打开指定路径的video文件

    File f = new File(filePath);
                    Intent intent = new Intent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(f), "video/*");
                    startActivity(intent);
  • 相关阅读:
    Luogu 1080 【NOIP2012】国王游戏 (贪心,高精度)
    Luogu 1314 【NOIP2011】聪明的质检员 (二分)
    Luogu 1315 【NOIP2011】观光公交 (贪心)
    Luogu 1312 【NOIP2011】玛雅游戏 (搜索)
    Luogu 1525 【NOIP2010】关押罪犯 (贪心,并查集)
    Luogu 1514 引水入城 (搜索,动态规划)
    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
    Luogu 1437 [HNOI2004]敲砖块 (动态规划)
    Luogu 1941 【NOIP2014】飞扬的小鸟 (动态规划)
    HDU 1176 免费馅饼 (动态规划)
  • 原文地址:https://www.cnblogs.com/GoAhead/p/2502276.html
Copyright © 2011-2022 走看看