android studio kotlin录音
时间: 2023-09-10 08:14:46 浏览: 307
kotlin android
在Android Studio中使用Kotlin进行录音可以通过使用`MediaRecorder`类来实现。以下是一个简单的示例代码:
```kotlin
import android.Manifest
import android.content.pm.PackageManager
import android.media.MediaRecorder
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.io.IOException
class MainActivity : AppCompatActivity() {
private val RECORD_AUDIO_PERMISSION = Manifest.permission.RECORD_AUDIO
private val WRITE_EXTERNAL_STORAGE_PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE
private val REQUEST_PERMISSION_CODE = 101
private var mediaRecorder: MediaRecorder? = null
private var isRecording = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recordButton = findViewById<Button>(R.id.record_button)
recordButton.setOnClickListener {
if (isRecording) {
stopRecording()
recordButton.text = "Start Recording"
} else {
if (checkPermissions()) {
startRecording()
recordButton.text = "Stop Recording"
}
}
}
}
private fun checkPermissions(): Boolean {
val permissionRecordAudio = ContextCompat.checkSelfPermission(
this, RECORD_AUDIO_PERMISSION
)
val permissionWriteExternalStorage = ContextCompat.checkSelfPermission(
this, WRITE_EXTERNAL_STORAGE_PERMISSION
)
val permissionsToRequest = ArrayList<String>()
if (permissionRecordAudio != PackageManager.PERMISSION_GRANTED) {
permissionsToRequest.add(RECORD_AUDIO_PERMISSION)
}
if (permissionWriteExternalStorage != PackageManager.PERMISSION_GRANTED) {
permissionsToRequest.add(WRITE_EXTERNAL_STORAGE_PERMISSION)
}
if (permissionsToRequest.isNotEmpty()) {
ActivityCompat.requestPermissions(
this, permissionsToRequest.toTypedArray(), REQUEST_PERMISSION_CODE
)
return false
}
return true
}
private fun startRecording() {
val filePath = "${Environment.getExternalStorageDirectory().absolutePath}/recording.3gp"
mediaRecorder = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
setOutputFile(filePath)
try {
prepare()
start()
isRecording = true
Toast.makeText(this@MainActivity, "Recording started", Toast.LENGTH_SHORT).show()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
private fun stopRecording() {
mediaRecorder?.apply {
stop()
reset()
release()
mediaRecorder = null
isRecording = false
Toast.makeText(this@MainActivity, "Recording stopped", Toast.LENGTH_SHORT).show()
}
}
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<out String>, grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (isRecording) {
startRecording()
}
} else {
Toast.makeText(
this,
"Permission denied. Please grant the necessary permissions.",
Toast.LENGTH_SHORT
).show()
}
}
}
}
```
请确保在使用之前在`AndroidManifest.xml`文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
这段代码创建了一个简单的界面,其中有一个按钮用于开始和停止录音。录制的音频文件将保存在设备的外部存储目录中,并以`recording.3gp`命名。请确保在运行应用程序之前为应用程序授予适当的权限。
希望这能帮到你!
阅读全文