Как записать видео и отобразить его в kotlin
Я работаю над приложением, которое позволяет пользователям записывать видео, и для этого я сделал камеру с помощью surface view, где я добавил свои собственные значки. Я хочу записать видео с помощью камеры и отобразить его в другом виде деятельности, и для этого я написал несколько кодов, но коды не работают.Вот мой код
class Chooses : Activity(), SurfaceHolder.Callback { private var recorder: MediaRecorder? = null private var surfaceHolder: SurfaceHolder? = null private var camcorderProfile: CamcorderProfile? = null private var camera: Camera? = null var recording = false var usecamera = true var previewRunning = false var surfaceView: SurfaceView? = null var btnStart: Button? = null var btnStop: Button? = null var root: File? = null var file: File? = null var isSDPresent: Boolean? = null var simpleDateFormat: SimpleDateFormat? = null var timeStamp: String? = null @SuppressLint("SourceLockedOrientationActivity") public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE) window.setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN ) camera?.setDisplayOrientation(90) val rotation: Int = this.windowManager.defaultDisplay.rotation var degrees = 0 when (rotation) { Surface.ROTATION_0 -> degrees = 0 Surface.ROTATION_90 -> degrees = 90 Surface.ROTATION_180 -> degrees = 180 Surface.ROTATION_270 -> degrees = 270 } var result: Int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT setContentView(R.layout.activity_chooses) initComs() actionListener() } // @SuppressLint("SimpleDateFormat") private fun initComs() { simpleDateFormat = SimpleDateFormat("ddMMyyyyhhmmss") timeStamp = simpleDateFormat!!.format(Date()) camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH) surfaceView = findViewById<View>(R.id.preview) as SurfaceView surfaceHolder = surfaceView!!.holder surfaceHolder?.addCallback(this) btnStop = findViewById<View>(R.id.btn_stop) as Button surfaceHolder?.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) isSDPresent = Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED } @SuppressLint("SdCardPath") private fun initRecorder() { recorder?.setAudioSource(MediaRecorder.AudioSource.DEFAULT) recorder?.setVideoSource(MediaRecorder.VideoSource.DEFAULT) val cpHigh = CamcorderProfile .get(CamcorderProfile.QUALITY_HIGH) recorder?.setProfile(cpHigh) recorder?.setOutputFile("/sdcard/videocapture_example.mp4") recorder?.setMaxDuration(50000) // 50 seconds recorder?.setMaxFileSize(5000000) // Approximately 5 megabytes } fun onClicks(v: View?) { if (recording) { recorder!!.stop() recording = false chronometer?.setTextColor(resources.getColor(android.R.color.holo_green_light)) chronometer?.stop() // Let's initRecorder so we can record again initRecorder() prepareRecorder() } else { recording = true recorder!!.start() chronometer?.base = SystemClock.elapsedRealtime() chronometer?.setTextColor(resources.getColor(android.R.color.holo_blue_dark)) chronometer?.start() } } private fun actionListener() { btnStop?.setOnClickListener { if (recording) { recorder!!.stop() if (usecamera) { try { camera!!.reconnect() } catch (e: IOException) { e.printStackTrace() } } recorder?.release(); recording = false // Let's prepareRecorder so we can record again prepareRecorder() } } } private fun prepareRecorder() { recorder = MediaRecorder() recorder!!.setPreviewDisplay(surfaceHolder!!.surface) if (usecamera) { camera?.unlock() recorder?.setCamera(camera) } recorder!!.setAudioSource(MediaRecorder.AudioSource.DEFAULT) recorder!!.setVideoSource(MediaRecorder.VideoSource.DEFAULT) recorder!!.setProfile(camcorderProfile) if (camcorderProfile!!.fileFormat == MediaRecorder.OutputFormat.MPEG_4) { recorder!!.setOutputFile( "/sdcard/XYZApp/" + "XYZAppVideo" + "" + SimpleDateFormat("ddMMyyyyHHmmss").format(Date()) + ".mp4" ) } else if (camcorderProfile!!.fileFormat == MediaRecorder.OutputFormat.MPEG_4) { recorder!!.setOutputFile( "/sdcard/XYZApp/" + "XYZAppVideo" + "" + SimpleDateFormat("ddMMyyyyHHmmss").format(Date()) + ".mp4" ) } else { recorder!!.setOutputFile( "/sdcard/XYZApp/" + "XYZAppVideo" + "" + SimpleDateFormat("ddMMyyyyHHmmss").format(Date()) + ".mp4" ) } try { recorder!!.prepare() } catch (e: IllegalStateException) { e.printStackTrace() finish() } catch (e: IOException) { e.printStackTrace() finish() } } override fun surfaceCreated(holder: SurfaceHolder?) { println("onsurfacecreated") if (usecamera) { camera = Camera.open() try { camera?.setPreviewDisplay(holder) camera?.startPreview() previewRunning = true } catch (e: IOException) { e.printStackTrace() } } } override fun surfaceChanged( holder: SurfaceHolder?, format: Int, width: Int, height: Int ) { println("onsurface changed") if (!recording && usecamera) { if (previewRunning) { camera?.stopPreview() } try { val p = camera?.parameters camcorderProfile?.videoFrameWidth?.let { p?.setPreviewSize( it, camcorderProfile!!.videoFrameHeight ) } if (p != null) { p.set("orientation", "portrait") } if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { camera?.setDisplayOrientation(90); } else { camera?.setDisplayOrientation(0); } camera?.parameters = p p?.previewFrameRate = camcorderProfile?.videoFrameRate!! camera?.parameters = p camera?.setPreviewDisplay(holder) camera?.startPreview() previewRunning = true } catch (e: IOException) { e.printStackTrace() } } } override fun surfaceDestroyed(holder: SurfaceHolder?) { if (recording) { recorder!!.stop() recording = false } recorder?.release() if (usecamera) { previewRunning = false camera?.lock(); camera?.release() } finish() } companion object { fun megabytesAvailable(f: File): Float { val stat = StatFs(f.path) val bytesAvailable = (stat.blockSize.toLong() * stat.availableBlocks.toLong()) return bytesAvailable / (1024f * 1024f) } } }
Что я уже пробовал:
Я пытаюсь записать видео с помощью камеры surface view и отобразить его в другой деятельности