英文:
How to record a video in Android allowing only landscape mode with a max time recording time
问题
仅翻译部分如下:
我只需要在Android设备上按照一些规格录制视频。然而,我真的感到很失望,因为无论我尝试什么奇怪的系统问题都会出现,然后我又回到了原点。所以我需要一个更有经验的开发者来指导我。
规格
我需要以下内容:
- 基本的视频界面(用户可以看到正在录制的内容)。
- 视频时间不能超过20秒(最长20秒,之后录制停止)。
- 仅允许横向模式。以某种方式阻止用户在纵向模式下录制。
- 配置按钮应该启用。设置按钮允许用户更改视频分辨率。
- 前置和后置摄像头按钮应该启用。
- 尽可能多的设备必须与我的应用程序兼容。
我已经尝试过的内容
下面的代码是我第一次尝试的。 当时我很高兴,因为它能够工作,但是它无法阻止纵向录制的视频。因此,我从这里的其他一些问题中了解到我应该使用camera2 Android API来解决这个问题。
Uri videoUri = FileProvider.getUriForFile(this, "com.my.myapplication.fileprovider", videofile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, VIDEO_DURATION);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 这不起作用
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LOCKED); // 这也不起作用
startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
所以我尝试了第二次。 我将代码改成了使用camera2 API,但是我发现大部分三星设备不支持它。这是我找到的信息来源。当然,在代码中出现一些奇怪的错误后,我才发现这一点。
我遵循了这个代码:androidwave代码
以及这个代码:lytcom/Camera2Kit
还有这个代码:googlearchive/android-Camera2Video
这些代码基本上是相同的。
由于设备不兼容camera2 API,它们不会返回正确的摄像头视频尺寸。我知道我的设备有一个1920x1080的分辨率摄像头,但系统方法返回的是:
E/Camera2VideoFragment: Width: 1440 Height 1080
E/Camera2VideoFragment: Width: 1088 Height 1088
E/Camera2VideoFragment: Width: 1472 Height 720
E/Camera2VideoFragment: Width: 1280 Height 720
E/Camera2VideoFragment: Width: 1056 Height 864
E/Camera2VideoFragment: Width: 960 Height 720
我的第三次尝试。 我使用了 JeroenMols/LandscapeVideoCamera 库,它提供了我所需的一切,包括在录制之前向用户显示旋转设备的精美图片。然而,这个库不适用于API>22,来源。
当前情况
现在,我不知道该怎么办了。无论我尝试什么都没有成功。而且大部分答案都是使用那些库。所以能不能有人帮帮我?请使用Java语言。我无法处理Kotlin。
英文:
I only need to record a video in Android device with some specifications. However, i'm really disappointed because everything that i try some weird system issue appeared and i go back to beginning. So i need a more experienced developer to guide me.
Specifications
I need the following:
- Basic video interface (user seeing what is recorded).
- Video time cannot exceed 20s (Max:20s after that recording stops).
- Only Landscape mode is enable. Block user somehow so he can't record in portrait mode.
- Configuration button should be enabled. Setting button that allow user to change video resolution
- Front and back camera button should be enabled.
- As many as possible, devices must be compatible with my application.
What i already tried
The below code was my first try. I was happy because it works, but it can't block portrait recording videos. So i read from some other question here that i should use camera2 android API for that.
Uri videoUri = FileProvider.getUriForFile(this, "com.my.myapplication.fileprovider", videofile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, VIDEO_DURATION);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // this won't work
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LOCKED); // neither this
startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
So i got my 2nd try. I changed my code to this camera2 API just to know that most of Samsung devices don't support it.
Here is my source to that information. Of course, i just discovered that after some weird mistakes appeared in my code.
I followed this code: androidwave code
And this code: lytcom/Camera2Kit
And this code: googlearchive/android-Camera2Video
Which are pretty most the same.
Since devices aren't compatible with camera2 API, they won't return good camera video size. I know that my device has a 1920x1080 resolution camera but the return from system method is
E/Camera2VideoFragment: Width: 1440 Height 1080
E/Camera2VideoFragment: Width: 1088 Height 1088
E/Camera2VideoFragment: Width: 1472 Height 720
E/Camera2VideoFragment: Width: 1280 Height 720
E/Camera2VideoFragment: Width: 1056 Height 864
E/Camera2VideoFragment: Width: 960 Height 720
My third try. I was using the library JeroenMols/LandscapeVideoCamera that gives me exactly what i need, including this beautiful picture that shows user to rotate device before recording. However this library won't work for API>22, source.
Currently
NOW, i don't know what to do. Whatever i tried didn't work. And most answer around just used those libraries. So can please somebody lend me a hand? Please Java language. I can't handle Kotlin.
答案1
得分: 0
因为我很匆忙,我使用了JeroenMols/LandscapeVideoCamera库来解决我的问题。可能是因为Android版本不兼容,我以后可能需要进行更改。尽管如此,我会在这里写出我的答案,也许会帮助其他人。
在使用这个库与新的Android版本(>=Android.M)时,主要问题是运行时权限。除了在清单中添加这些权限之外,我还在运行时请求了所有必要的权限:相机、音频和存储写入。
LandscapeVideoCamera的代码
void startCamera(){
CaptureConfiguration.Builder builder = new CaptureConfiguration.Builder(width, height, bitrate);
builder.maxDuration(20);
builder.frameRate(24);
builder.showRecordingTime();
CaptureConfiguration configuration = builder.build();
final Intent intent = new Intent(this, VideoCaptureActivity.class);
intent.putExtra(VideoCaptureActivity.EXTRA_CAPTURE_CONFIGURATION, configuration);
intent.putExtra(VideoCaptureActivity.EXTRA_OUTPUT_FILENAME, videofile.getAbsolutePath());
startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
}
请求权限
public void launchCamera(View v){
//首先检测SDK是否为Marshmallow
// 如果是,我们需要在运行时请求权限
// 首先检查我们是否已经获得了权限
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestStoragePermission();
return;
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestCameraPermission();
return;
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
requestAudioRecordPermission();
return;
}
startCamera();
}
请求权限结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == STORAGE_PERMISSION_CODE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// 权限已给予
startCamera();
}
else{
// 权限未给予
}
}
if(requestCode == CAMERA_PERMISSION_CODE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// 权限已给予
startCamera();
}
else{
// 权限未给予
}
}
if(requestCode == AUDIO_PERMISSION_CODE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
startCamera();
}
else{
// 权限未给予
}
}
}
英文:
Since i'm in a rush, i solved my problem with JeroenMols/LandscapeVideoCamera library. Maybe because of Android version incompatibility i will need to change later. Despite that, i will write my answer here, maybe will help someone else.
The main problem when using this library with new android version (>=Android.M) is the runtime permissions. Besides adding those permissions to the manifest, i requested all necessary permissions in runtime: Camera, Audio and Storage write.
Code for LandscapeVideoCamera
void startCamera(){
CaptureConfiguration.Builder builder = new CaptureConfiguration.Builder(width, height, bitrate);
builder.maxDuration(20);
builder.frameRate(24);
builder.showRecordingTime();
CaptureConfiguration configuration = builder.build();
final Intent intent = new Intent(this, VideoCaptureActivity.class);
intent.putExtra(VideoCaptureActivity.EXTRA_CAPTURE_CONFIGURATION, configuration);
intent.putExtra(VideoCaptureActivity.EXTRA_OUTPUT_FILENAME, videofile.getAbsolutePath());
startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
}
Asking Permissions
public void launchCamera(View v){
//first test if SDK is Marshmallow
// if it's then we need to ask permission during run time
//first check if we already got permission
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestStoragePermission();
return;
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestCameraPermission();
return;
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
requestAudioRecordPermission();
return;
}
startCamera();
}
}
Request Permission Result
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == STORAGE_PERMISSION_CODE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// permission given
startCamera();
}
else{
// permission not given
}
}
if(requestCode == CAMERA_PERMISSION_CODE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// permission given
startCamera();
}
else{
// permission not given
}
}
if(requestCode == AUDIO_PERMISSION_CODE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
startCamera();
}
else{
// permission not given
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论