android 录制视频及调取本地视频

这是今天要实现的功能


我是使用的ActionSheetDialog,可自行百度


在按钮里面添加showVideoDialog();方法,弹出视频选择对话框

private void showVideoDialog() {
    final String[] stringItems = {"录制视频", "本地视频",};
    final ActionSheetDialog sheetDialog = new ActionSheetDialog(QualificationExamActivity.this,
            stringItems, null);
    sheetDialog.title("上传视频")//
            .titleTextSize_SP(14.5f)//
            .show();

    sheetDialog.setOnOperItemClickL(new OnOperItemClickL() {
        private Intent intent;

        @Override
        public void onOperItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            if (position == 0) {
                intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);// 创建一个请求视频的意图
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);// 设置视频的质量,值为0-1,
                intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);// 设置视频的录制长度,s为单位
                intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 20 * 1024 * 1024L);// 设置视频文件大小,字节为单位
                startActivityForResult(intent, RequestCode.VIDEO_RECORD_REQUEST);// 设置请求码,在onActivityResult()方法中接收结果
            } else if (position == 1) {
                intent = new Intent(QualificationExamActivity.this, VideoListActivity.class);
                startActivityForResult(intent, 101);
            }
            sheetDialog.dismiss();
        }
    });
}

点击“录制视频”就可以调起自带的相机进行视频录制及播放,“本地视频”效果如下:(点击图片进行该视频的播放,选中item则返回主界面进行上传处理)



VideoInfo

public class VideoInfo {

    private String filePath;
    private String mimeType;
    private Bitmap b;
    private String title;
    private String time;
    private String size;

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public Bitmap getB() {
        return b;
    }

    public void setB(Bitmap b) {
        this.b = b;
    }
}

创建一个本地视频列表VideoListActivity

使用ContentResolver获取本地的音频信息


import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

@ContentView(R.layout.activity_video_list)
public class VideoListActivity extends ActivityBasic {

    @ViewInject(R.id.lv_video)
    ListView mListView;

    @ViewInject(R.id.tv_nodata)
    TextView noData;

    ArrayList<VideoInfo> vList;
    private Intent lastIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        lastIntent = getIntent();
        initData();
        mListView.setOnItemClickListener(new ItemClick());
    }

    private void initData() {
        vList = new ArrayList<VideoInfo>();
        String[] mediaColumns = new String[]{MediaStore.MediaColumns.DATA,
                BaseColumns._ID, MediaStore.MediaColumns.TITLE, MediaStore.MediaColumns.MIME_TYPE,
                MediaStore.Video.VideoColumns.DURATION, MediaStore.MediaColumns.SIZE};
        Cursor cursor = getContentResolver().query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns,
                null, null, null);
        if (cursor.moveToFirst()) {
            do {
                VideoInfo info = new VideoInfo();
                info.setFilePath(cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)));
                info.setMimeType(cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE)));
                info.setTitle(cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE)));
                info.setTime(CommTools.LongToHms(cursor.getInt(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION))));
                info.setSize(CommTools.LongToPoint(cursor
                        .getLong(cursor
                                .getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE))));
                int id = cursor.getInt(cursor
                        .getColumnIndexOrThrow(BaseColumns._ID));
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inDither = false;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                info.setB(MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id,
                        MediaStore.Images.Thumbnails.MICRO_KIND, options));
                vList.add(info);

            } while (cursor.moveToNext());
        }

            VideoListAdapter adapter = new VideoListAdapter(this, vList);
            mListView.setAdapter(adapter);

    }

    private class ItemClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mListView.getItemAtPosition(position);
            String filePath = vList.get(position).getFilePath();
            lastIntent.putExtra("path", filePath);
            setResult(102, lastIntent);
            finish();
        }

    }

    class VideoListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private ArrayList<VideoInfo> vList;

        public VideoListAdapter(Context context, ArrayList<VideoInfo> videoList) {
            this.inflater = LayoutInflater.from(context);
            this.setVideoList(videoList);
        }

        public void setVideoList(ArrayList<VideoInfo> videoList) {
            this.vList = videoList;
        }

        @Override
        public int getCount() {
            return vList.size();
        }

        @Override
        public Object getItem(int p) {
            return vList.get(p);
        }

        @Override
        public long getItemId(int p) {
            return p;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item_video_list, null);
                holder.vImage = (ImageView) convertView.findViewById(R.id.video_img);
                holder.vTitle = (TextView) convertView.findViewById(R.id.video_title);
                holder.vSize = (TextView) convertView.findViewById(R.id.video_size);
                holder.vTime = (TextView) convertView.findViewById(R.id.video_time);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.vImage.setImageBitmap(vList.get(position).getB());
            holder.vTitle.setText(vList.get(position).getTitle()); 
            holder.vSize.setText(vList.get(position).getSize());
            holder.vTime.setText(vList.get(position).getTime());

            holder.vImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    String bpath = "file://" + vList.get(position).getFilePath();
                    intent.setDataAndType(Uri.parse(bpath), "video/*");
                    startActivity(intent);
                }
            });
            return convertView;
        }

        class ViewHolder {
            ImageView vImage;
            TextView vTitle;
            TextView vSize;
            TextView vTime;
        }
    }


}


视频录制/选择完成,回到主页面,通过onActivityResult()接收传过来的视频信息

private String filPaths;

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

    switch (requestCode) {
        case RequestCode.VIDEO_RECORD_REQUEST:
            if (null != data) {
                Uri uri = data.getData();
                if (uri == null) {
                    return;
                } else {
                    Cursor c = getContentResolver().query(uri,
                            new String[]{MediaStore.MediaColumns.DATA},
                            null, null, null);
                    if (c != null && c.moveToFirst()) {
                        filPaths = c.getString(0);
                        showUploadVideoDialog();
                    }
                }
            }
            break;
        case 101:
            if (resultCode == 102 && data != null) {
                filPaths = data.getStringExtra("path");
                showUploadVideoDialog();
            }
            break;
        default:
            break;
    }

   }


完成


本来是因为做项目有视频这一块的需求,所以就截取了代码过来写博客记录,没想到有人要源码啊:)

将视频这一块抽出来写了个demo,ActionSheetDialog代码较多,全部放在了dialog包内,也可以不用这个,自己弄个弹出的界面,用popupwindows之类的。。。。

资源上传到了CSDN,GitHub我还玩不溜啊~~附上下载页地址吧视频录制及本地视频调取



阅读更多
换一批

没有更多推荐了,返回首页