下面是一个使用Kotlin在Android中实现视频捕获并上传到Firebase的示例代码:
首先,确保在build.gradle文件中添加了Firebase和CameraX的依赖项:
dependencies {
implementation 'com.google.firebase:firebase-storage:20.0.0'
implementation 'androidx.camera:camera-camera2:1.1.0-alpha02'
implementation 'androidx.camera:camera-lifecycle:1.1.0-alpha02'
implementation 'androidx.camera:camera-view:1.0.0-alpha21'
}
然后,在AndroidManifest.xml文件中添加相机和存储权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
接下来,创建一个名为CameraActivity的新Activity,并在布局文件中添加一个预览视图和一个按钮来启动视频捕获:
class CameraActivity : AppCompatActivity() {
private lateinit var cameraView: PreviewView
private lateinit var captureButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_camera)
cameraView = findViewById(R.id.cameraView)
captureButton = findViewById(R.id.captureButton)
captureButton.setOnClickListener {
captureVideo()
}
}
private fun captureVideo() {
val videoFile = File(externalMediaDirs.first(), "video.mp4")
val videoCapture = VideoCapture.Builder()
.setTargetRotation(cameraView.display.rotation)
.setTargetAspectRatio(AspectRatio.RATIO_16_9)
.setTargetResolution(Size(1280, 720))
.build()
videoCapture.startRecording(videoFile, Executors.newSingleThreadExecutor(), object : VideoCapture.OnVideoSavedCallback {
override fun onVideoSaved(file: File) {
uploadVideo(file)
}
override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) {
Toast.makeText(this@CameraActivity, "视频捕获错误: $message", Toast.LENGTH_SHORT).show()
}
})
}
private fun uploadVideo(file: File) {
val storageRef = FirebaseStorage.getInstance().reference
val videoRef = storageRef.child("videos/${file.name}")
val uploadTask = videoRef.putFile(file)
uploadTask.addOnSuccessListener {
Toast.makeText(this@CameraActivity, "视频上传成功", Toast.LENGTH_SHORT).show()
}.addOnFailureListener {
Toast.makeText(this@CameraActivity, "视频上传失败: ${it.message}", Toast.LENGTH_SHORT).show()
}
}
}
最后,创建一个名为activity_camera.xml的布局文件,其中包含一个PreviewView和一个Button:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@id/captureButton"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/captureButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="开始捕获视频"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这个示例代码将在点击按钮时开始视频捕获,并将捕获的视频文件上传到Firebase存储中。请确保在Firebase控制台上设置了正确的存储规则,并在AndroidManifest.xml中添加了适当的权限。
希望这可以帮助你解决问题!