欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android仿微信小视频录制_高仿微信10秒视频拍摄

程序员文章站 2022-03-01 12:52:02
...

Android仿微信视频录制功能、并同时带有播放功能、可以实现10秒中的视频录制、和微信一样也是按住一个圆形的按键不放就可以拍摄了、然后将该视频以mp4的格式存放在本地、用surfaceview播放

该Demo只是实现了一个粗糙的功能、如果要集成到自己的项目里面还需要修改的、所以下载的哥们就不要嫌弃Demo比较简单和有bug了、有需要的哥们可以下载看看、希望会对大家有帮助、下面是运行的效果图

Android仿微信小视频录制_高仿微信10秒视频拍摄


onCreate代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    btnRecordAudio = (Button) findViewById(R.id.btn_record_audio);
    btnPlay = (ImageButton) findViewById(R.id.play);
    File filePathFile = new File("/storage/emulated/0/im/video/");
    if (filePathFile != null &&filePathFile.listFiles()!=null) {
        if (filePathFile.listFiles().length > 0) {
            path = filePathFile.listFiles()[0].getPath();
            Bitmap bitmap = Utils.createVideoThumbnail(path);
            BitmapDrawable drawable = new BitmapDrawable(bitmap);
            drawable.setTileModeXY(Shader.TileMode.REPEAT,
                    Shader.TileMode.REPEAT);
            drawable.setDither(true);
            btnPlay.setBackgroundDrawable(drawable);
        }
    }
    btnRecordAudio.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 启动拍摄的Activity
            Intent intent = new Intent(FirstActivity.this,MainActivity.class);
            FirstActivity.this.startActivityForResult(intent,200);
        }
    });

    btnPlay.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {
            // 显示播放页面
            if (path!=null&&!path.equalsIgnoreCase("")) {
                VideoFragment bigPic = VideoFragment.newInstance(path);
                android.app.FragmentManager mFragmentManager = getFragmentManager();
                FragmentTransaction transaction = mFragmentManager.beginTransaction();
                transaction.replace(R.id.main_menu, bigPic);
                transaction.commit();
            }
        }
    });
}


onActivityResult代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case 200:
            if(resultCode == RESULT_OK) {
                // 成功
                path = data.getStringExtra("path");
                Toast.makeText(FirstActivity.this,"存储路径为:" path,Toast.LENGTH_SHORT).show();
                // 通过路径获取第一帧的缩略图并显示
                Bitmap bitmap = Utils.createVideoThumbnail(path);
                BitmapDrawable drawable = new BitmapDrawable(bitmap);
                drawable.setTileModeXY(Shader.TileMode.REPEAT , Shader.TileMode.REPEAT);
                drawable.setDither(true);
                btnPlay.setBackgroundDrawable(drawable);
            } else {
                // 失败
            }
            break;

    }
}


VideoFragment onCreateView代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_video, container, false);
    mVideoView = (VideoView) view.findViewById(R.id.video_view);
    btnClose = (Button) view.findViewById(R.id.btn_close);
    // 播放相应的视频
    mVideoView.setMediaController(new MediaController(getActivity()));
    mVideoView.setVideoURI(Uri.parse(videoPath));
    mVideoView.start();
    //mVideoView.requestFocus();

    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFragmentManager().beginTransaction().remove(VideoFragment.this).commit();
        }
    });
    return view;
}


最后给贴上Demo的源代码、希望对大家有用、有兴趣的哥们可以下载看看

源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1pJELvF9 密码: 6tmp