`
kldwq2002
  • 浏览: 9215 次
文章分类
社区版块
存档分类
最新评论

Record video

阅读更多
import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.hardware.Camera;
import android.hardware.Camera.Size;

import android.media.MediaRecorder;
import android.os.Bundle;

import android.util.Log;

import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class prova extends Activity implements SurfaceHolder.Callback{

   private SurfaceView mSurfaceView;
   private SurfaceHolder mSurfaceHolder;
   private Camera mCamera;
   private boolean mPreviewRunning;

   private static final String TAG = "RECORD";

   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSurfaceView = (SurfaceView)findViewById(R.id.camera_surface);
        mSurfaceHolder = mSurfaceView.getHolder();      //recupero l'holder della surfaceview
        mSurfaceHolder.addCallback(this);       //faccio la bind alla nostra activity
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        //tipo di surface, suggerito nei tutorial ufficiali

        final ImageButton buttonPictureR = (ImageButton) findViewById(R.id.camera_surface_buttonR);
        final ImageButton buttonPictureS = (ImageButton) findViewById(R.id.camera_surface_buttonS);
                buttonPictureR.setOnClickListener(new OnClickListener(){
                        public void onClick(View v) {

                           startRecording();
                           buttonPictureR.setVisibility(4);
                         buttonPictureS.setVisibility(0);


                        }
                });


                   buttonPictureS.setOnClickListener(new OnClickListener(){
                        public void onClick(View v) {

                              stopRecording();
                               buttonPictureR.setVisibility(0);
                             buttonPictureS.setVisibility(4);

                        }
                });                

   }

   public void surfaceCreated(SurfaceHolder holder) {

       mCamera = Camera.open();

    }

   public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        if (mPreviewRunning)
                mCamera.stopPreview();

        //setto le preferenze
        Camera.Parameters p = mCamera.getParameters();  //prendo le preferenze della camera

        p.setPreviewSize(arg2, arg3);
        //p.setPreviewFormat(PixelFormat.JPEG);
        ArrayList list = (ArrayList) p.getSupportedPictureSizes();  //recuepro le risoluzioni supportate dalla camera
        List list2 = p.getSupportedFocusModes();  //recuepro le risoluzioni supportate dalla camera
        int picture_width = list.get(0).width;
        int picture_height = list.get(0).height;
        p.setFocusMode(list2.get(0));

        p.setPictureSize(picture_width, picture_height);

        // salvo le pref
        mCamera.setParameters(p);
        try {

           System.out.println("sono fuori");

           //lancio la preview
           mCamera.setPreviewDisplay(arg0);        
            mCamera.startPreview();
            mPreviewRunning = true;
        } catch (IOException e) {
           System.out.println("sono dentro");
                //gestione errore
        }

   }

   public void surfaceDestroyed(SurfaceHolder holder) {

       mCamera.stopPreview();
       mPreviewRunning = false;
       mCamera.release();

    }

   private MediaRecorder mediaRecorder;
   private final int MAX_TIME = 20000;

   public boolean startRecording(){
      try {

         mediaRecorder = new MediaRecorder();
         mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
         mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
         mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
         mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
         mediaRecorder.setMaxDuration((int) MAX_TIME); 
         mediaRecorder.setVideoSize(320, 240);
         mediaRecorder.setVideoFrameRate(20); 
         mediaRecorder.setOutputFile("/sdcard/recordvideooutput.3gp");

         mediaRecorder.prepare();
         mediaRecorder.start();

         return true;
      } catch (IllegalStateException e) {
         Log.e(TAG,e.getMessage());
         e.printStackTrace();
         return false;
      } catch (IOException e) {
         Log.e(TAG,e.getMessage());
         e.printStackTrace();
         return false;
      }
   }

   public void stopRecording(){
      mediaRecorder.stop();
      mediaRecorder.release(); // Now the object cannot be reused
   }
}


判断SD卡是否加载

if (android.os.Environment.getExternalStorageState() != android.os.Environment.MEDIA_MOUNTED)


public class VideoActivity extends Activity {

private SurfaceView preview;
private SurfaceHolder previewHolder;
private String locationName;
private String filepath;
private File video;

public void onCreate(Bundle videocawk) {
    super.onCreate(videocawk);
    setContentView(R.layout.video_layout);
    setSurface();
    locationName = getIntent().getStringExtra("locationName");
    filepath = getFilePath(locationName);
    try {
        MediaRecorder r = getMediaRecorder(filepath, previewHolder
                .getSurface());
        setSurfaceCallback(preview,r);
        setButtonListeners(r);
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private String getFilePath(String locName) {
    String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
    String add = "/test/data/video/";
    String name = locName + "--1";
    String total = dir + add + name;
    video = new File(total);
    return total;
}

private void setSurface() {
    preview = (SurfaceView) findViewById(R.id.preview);
    previewHolder = preview.getHolder();
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

private void setButtonListeners(final MediaRecorder r) {
    Button start = (Button) findViewById(R.id.start_video);
    Button end = (Button) findViewById(R.id.stop_video);

    start.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            startRecording(r);

        }
    });

    end.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            stopRecording(r);
            setPassPrefs();
            startActivity(setPassPrefs());
            finish();

        }
    });

}

private void setSurfaceCallback(SurfaceView s, final MediaRecorder r)
{


SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {

        try {
            r.setPreviewDisplay(previewHolder.getSurface());
        } catch (Throwable t) {
            Log.e("PictureDemo-surfaceCallback",
                    "Exception in setPreviewDisplay()", t);
            Toast.makeText(VideoActivity.this, t.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        r.stop();
        r.release();

    }
};

previewHolder.addCallback(surfaceCallback); 
}

private Intent setPassPrefs() {
    AttachedImageAdapter adapter = new AttachedImageAdapter(locationName,
            VideoActivity.this);
    adapter.setVideoPath(filepath);
    Intent i = new Intent(VideoActivity.this, EnterTag.class);
    i.putExtras(getIntent());
    return i;

}

private void startRecording(MediaRecorder r) {
    r.start();
}

private void stopRecording(MediaRecorder r) {
    r.stop();
}

private MediaRecorder getMediaRecorder(String filepath, Surface s)
        throws IllegalStateException, IOException {
    MediaRecorder m_recorder = new MediaRecorder();
    m_recorder.setPreviewDisplay(s);
    m_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    m_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    m_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    m_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    m_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    m_recorder.setMaxDuration(20000); // length of video in MS
    m_recorder.setVideoSize(320, 240);
    m_recorder.setVideoFrameRate(15);
    m_recorder.setOutputFile(video.getPath());
    m_recorder.prepare();

    return m_recorder;
}
}
分享到:
评论

相关推荐

    前端项目-videojs-record.zip

    **前端项目 - videojs-record 插件详解** `videojs-record` 是一个强大的前端插件,主要用于在Web浏览器中实现音频、视频以及图像的录制功能。这个插件基于流行的 HTML5 视频库 `video.js`,为开发者提供了一种简单...

    record video from webcam using matlab:record video from webcam using matlab-matlab开发

    在 MATLAB 中录制网络摄像头的视频是一项实用且常见...`recording_video.zip` 文件可能包含了一个示例代码,解压后运行即可体验这一过程。如果你遇到任何问题,MATLAB 的官方文档和社区资源都是很好的学习和求助渠道。

    Android代码-DcCam

    And if you want to add a limit to user , that user should only able to record video up to certain length. Like instagram then this demo is for you. Pros Its handles orientations by its own. For both...

    Android代码-高仿抖音照片电影功能。

    中文简介 PhotoMovie can easily achieve the function of PhotoMovie like TikTok. The functions are shown below. Filter Transition ...Before:------------Record Video------------>------

    Fraps_3.4.6_Retail

    Realtime Video Capture Software - Have you ever wanted to record video while playing your favourite game? Come join the Machinima revolution! Throw away the VCR, forget about using a DV cam, game ...

    超市闭路电视监控系统解决方案.doc

    The recording device, a 24-hour digital video recorder, can record video signals for an extended period of time. In conclusion, the 超市闭路电视监控系统解决方案 is a comprehensive solution that ...

    Record_Audio_Video.py

    python 利用网络摄像头和麦克风指定时间录取视频, 主要利用的是pyaudio音频处理库、cv2视频处理库, 加上多线程同时录音和录像保证最后合成的视频一致。

    video-record-demo_html5_DEMO_

    "video-record-demo_html5_DEMO_"这个项目就是关于如何在HTML5环境下实现视频录制的一个示例。 首先,HTML5的MediaStream API允许访问用户的摄像头和麦克风。`<input type="file" capture>`元素或者`navigator....

    videojs-record:video.js插件,用于记录音频视频图像文件

    videojs记录一个插件,用于记录音频/视频/图像文件。文献资料该文档和示例可以在以下位置找到: :捐财务贡献者成为财务贡献者,并帮助我们维持我们的社区。 [贡献] 个人 组织机构与您的组织一起支持该项目。 您的...

    BlackBerry For Dummies 第三版

    Take photos, record video, and sync your BlackBerry with iTunes Back up your BlackBerry, arrange automated backups, maintain your battery, and protect your information with secure passwords Use your ...

    scrcpy-win64-v1.14.zip android 投屏软件,不需 要在android中安装APK

    在这个例子中,`-m` 参数指定了屏幕大小,`-b` 设置了传输速率,`-r` 设定了帧率,`--record` 开启了录屏功能,指定输出的视频文件名为`video.mp4`。 总的来说,Scrcpy-win64-v1.14提供了一个高效且便捷的Android...

    workspace_record_video.zip

    这个"workspace_record_video.zip"压缩包文件包含了实现这一功能的关键代码。以下是对这个项目的一些详细解析: 首先,"微信拍摄小视频"的核心是利用Android原生控件来实现视频的录制和播放。在Android中,...

    一个只有几百K大小的绿色刻录软件

    Record BIN/CUE Image 顾名思义,将cue/bin格式的映像文件刻录成光盘 Record Generic Image 将常用的映像文件刻录成光盘,支持这几种格式:iso、ima、bin、udi、udf Record VIDEO_TS 刻录DVD影碟  Erase Disc 清除...

    基于Accord.net的人脸识别与单帧视频捕获 C#项目示例

    基于Accord.net的人脸识别与单帧视频捕获 C#项目示例视频捕获代码思路来源于Record video with Accord.net (AForge)

    TransCAD菜单列表翻译[文].pdf

    Symbol Theme)、着色(Coloring)、特征显示(Feature Display)、三维地图(Prism Map)、展示隐藏标签(Display Hidden Labels)、恢复所有标签(Restore All Labels)和录制视频(Record Video)。 4. **...

    small-video-record:利用FFmpeg视频录制微信小视频伴随压缩处理

    底层使用FFmpeg进行压缩处理(在small-video-record2中,C源代码和Java源代码都是开放的)。 效果如下: 指示: 特征: 编码时收集。 使用FFmpeg自定义录制具有不同时间,分辨率,比特率,帧速率和转码速度的...

    js实现屏幕截图视频录制 video_record.7z

    本项目"js实现屏幕截图视频录制 video_record.7z"提供了这样一个功能,它允许用户在同源策略的限制下进行视频的截图和录制,但无法跨域录制来自其他网站的媒体源。以下是对这个功能的详细解释和相关知识点的介绍。 ...

    Android代码-Lollipop相机API实例

    This sample shows how to record video using the new Camera2 API in Android Lollipop. Introduction Android Lollipop introduced a new camera API, called camera2. This sample uses CameraDevice and ...

    ffmpeg-0.11.1.rar_ffmpeg audio_video record

    这个“ffmpeg-0.11.1.rar_ffmpeg audio_video record”压缩包显然包含了FFmpeg的一个特定版本——0.11.1,用于音频和视频的录制、转换以及流媒体传输。FFmpeg在IT行业中扮演着至关重要的角色,因为它是多媒体处理...

    OBS Video Record

    **OBS Video Record** OBS(Open Broadcasting Software)是一款免费且开源的屏幕录制与直播软件,广泛应用于视频创作者、教育者、游戏主播等群体。它提供了高质量的视频录制和直播功能,用户可以通过简单的操作...

Global site tag (gtag.js) - Google Analytics