避免将视频序列化并保存在设备上-Android
创始人
2024-12-16 08:30:03
0

在Android上,可以使用MediaRecorder和Camera类来避免将视频序列化并保存在设备上。下面是一个简单的示例代码,演示如何使用Camera类录制视频并实时播放:

首先,需要在AndroidManifest.xml文件中添加相应的权限:




然后,在Activity的布局文件中添加一个SurfaceView用于显示预览:


接下来,可以在Activity中使用Camera类来实现视频录制和预览:

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private Camera camera;
    private SurfaceHolder surfaceHolder;
    private MediaRecorder mediaRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SurfaceView surfaceView = findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            camera = Camera.open();
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (surfaceHolder.getSurface() == null) {
            return;
        }

        try {
            camera.stopPreview();
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    public void startRecording(View view) {
        if (prepareMediaRecorder()) {
            mediaRecorder.start();
        }
    }

    public void stopRecording(View view) {
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.reset();
            mediaRecorder.release();
            mediaRecorder = null;
        }
    }

    private boolean prepareMediaRecorder() {
        mediaRecorder = new MediaRecorder();
        camera.unlock();
        mediaRecorder.setCamera(camera);

        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

        File outputDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
        File outputFile = new File(outputDir, "video.mp4");

        mediaRecorder.setOutputFile(outputFile.getAbsolutePath());

        mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

        try {
            mediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

在上面的示例中,surfaceCreated方法中打开了相机,并将预览显示到SurfaceView上。startRecording和stopRecording方法用于开始和结束录制。prepareMediaRecorder方法用于配置MediaRecorder对象。

需要注意的是,在Android 6.0及以上版本,需要在运行时请求相应的权限。可以使用以下代码来请求相机和录音权限:

private static final int REQUEST_PERMISSIONS = 1;
private static final String[] PERMISSIONS = {
        Manifest.permission.CAMERA,
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

private void requestPermissions() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(PERMISSIONS[0]) != PackageManager.PERMISSION_GRANTED ||
                checkSelfPermission(PERMISSIONS[1]) != PackageManager.PERMISSION_GRANTED ||
                checkSelfPermission(PERMISSIONS[2]) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(PERMISSIONS, REQUEST_PERMISSIONS);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_PERMISSIONS && grantResults.length > 0) {
        boolean allGranted = true;
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                allGranted = false;
                break;
            }
        }

        if (!allGranted) {
            Toast.makeText(this, "Permissions not granted", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}

相关内容

热门资讯

十分钟辅助挂!钱塘十三水有没有... 十分钟辅助挂!钱塘十三水有没有套路(辅助挂)详细辅助挂教程(2024已更新)(抖音)1、用户打开应用...
八分钟辅助挂!佳友互娱有挂吗(... 八分钟辅助挂!佳友互娱有挂吗(透视)详细辅助app教程(2023已更新)(知乎);该软件可以轻松地帮...
十分钟辅助挂!哈狗十三道怎么玩... 十分钟辅助挂!哈狗十三道怎么玩胜率大(透视)详细辅助神器教程(2025已更新)(今日头条)1、上手简...
4分钟辅助挂!兴动互娱辅助器怎... 4分钟辅助挂!兴动互娱辅助器怎么收费(辅助挂)详细辅助软件教程(2021已更新)(哔哩哔哩)兴动互娱...
十分钟辅助挂!胡乐麻将胡牌神器... 十分钟辅助挂!胡乐麻将胡牌神器(透视)详细辅助挂教程(2025已更新)(抖音)胡乐麻将胡牌神器软件透...
8分钟辅助挂!闲来麻将输赢靠系... 8分钟辅助挂!闲来麻将输赢靠系统吗(透视)详细辅助APP教程(2025已更新)(抖音);闲来麻将输赢...
7分钟辅助挂!拱趴大菠萝免费辅... 7分钟辅助挂!拱趴大菠萝免费辅助器(辅助挂)详细辅助脚本教程(2021已更新)(小红书)1、玩家可以...
1分钟辅助挂!哈灵麻将辅牌器购... 1分钟辅助挂!哈灵麻将辅牌器购买(辅助挂)详细辅助软件教程(2021已更新)(今日头条)1.哈灵麻将...
五分钟辅助挂!网易棋牌麻将有挂... 五分钟辅助挂!网易棋牌麻将有挂吗(辅助挂)详细辅助器教程(2020已更新)(今日头条)暗藏猫腻,小编...
两分钟辅助挂!微信小程序三打哈... 两分钟辅助挂!微信小程序三打哈辅助器(透视)详细辅助攻略教程(2024已更新)(今日头条)在进入微信...