我正在尝试使用模拟器中的相机录制视频。录制后,我希望VideoView在剪辑中显示1毫秒的预览图像。然后在VideoView上添加MediaController。用户必须单击一个按钮来创建激活ACTION_VIDEO_CAPTURE的意图。请参阅用户界面布局here。
我可以在模拟器的相机应用程序中进行录制,但是问题是我什至无法立即在VideoView中回放它。在第一段中我如何实现我的目标?我没有从设备或Logcat中收到任何错误消息。录制后,它只是返回到空白的videoView。可以是视频格式吗?
仅供参考,我在同一屏幕上有一个ImageView,效果很好。
onCreate方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_card);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Create a new activity card");
activity_title = findViewById(R.id.activity_title);
imageView = findViewById(R.id.imageView);
editText_hrs = findViewById(R.id.editText_hrs);
editText_mins = findViewById(R.id.editText_mins);
videoBtn = findViewById(R.id.uploadVideo);
videoView = findViewById(R.id.videoView);
checkbox = findViewById(R.id.check_useofitems);
createcard_done = findViewById(R.id.createcard_done);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
imageutils = new ImageUtils(this);
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
imageutils.imagepicker(1);
}
});
videoUtils = new VideoUtils(this);
videoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//videoUtils.videoPicker(1);
//videoView.setVideoURI(videoUtils.getVideoUri());
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
if(videoIntent.resolveActivity(getPackageManager()) != null){
startActivityForResult(videoIntent, VIDEO_REQUEST);
}
}
});
videoView.setVideoURI(videoUri);
videoView.start();
onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
imageutils.onActivityResult(requestCode, resultCode, data);
break;
case 1:
if (resultCode == RESULT_OK) {
videoUri = data.getData();
}
break;
}
}
答案 0 :(得分:0)
好的,感谢天,我设法解决了我的问题。问题在于我如何处理 onActivityResult()中的requestCode案例。我在同一活动中有一个ImageView和VideoView。我的ImageView和VideoView都允许用户从相机或图库上载。
这是在 onCreate()方法中处理ImageView和VideoView的代码:
imageutils = new ImageUtils(this);
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
imageutils.imagepicker(1);
}
});
videoUtils = new VideoUtils(this);
videoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
videoUtils.videoPicker(1);
}
});
在ImageUtils类中,我有一个 camera_call()和一个 gallery_call():
public void camera_call() {
ContentValues values = new ContentValues();
imageUri = current_activity.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
current_activity.startActivityForResult(intent1, 0);
}
public void gallery_call() {
Intent intent2 = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent2.setType("image/*");
current_activity.startActivityForResult(intent2, 1);
}
在VideoUtils类中,我还有一个 camera_call()和一个 gallery_call():
public void camera_call() {
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
current_activity.startActivityForResult(videoIntent, 2);
}
public void gallery_call() {
Intent intent2 = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
intent2.setType("video/*");
current_activity.startActivityForResult(intent2, 3);
}
您知道,startActivityForResult的参数如下: startActivityForResult(intent,requestCode)。因此,这就是我使用 onActivityResult():
在同一活动中处理ImageView和VideoView中相机和图库的不同requestCodes的方式:@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
if(requestCode == 0 || requestCode == 1) {
imageutils.onActivityResult(requestCode, resultCode, data);
}
if(requestCode == 2 || requestCode == 3) {
videoView.setVisibility(View.VISIBLE);
videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.setFocusable(true);
videoView.seekTo(1);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
Log.i("True", "Executed");
}
}
}
就是这样!现在,我可以在活动中上载和显示图像,并在同一活动中上载和播放视频。