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 lateinit var mediaRecorder: MediaRecorder
private var outputFile: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recordButton: Button = findViewById(R.id.record_button)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), 1)
}
outputFile = Environment.getExternalStorageDirectory().absolutePath + "/recording.3gp"
mediaRecorder = MediaRecorder()
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC)
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
mediaRecorder.setOutputFile(outputFile)
recordButton.setOnClickListener {
try {
mediaRecorder.prepare()
mediaRecorder.start()
Toast.makeText(this, "Recording started", Toast.LENGTH_SHORT).show()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
override fun onStop() {
super.onStop()
mediaRecorder.stop()
mediaRecorder.release()
Toast.makeText(this, "Recording stopped", Toast.LENGTH_SHORT).show()
}
}
Hiç yorum yok:
Yorum Gönder