Reputation: 29
i am new to kotlin,i got below code from https://developer.android.com/guide/topics/media/mediarecorder#kotlin. i wish to create button GUI from .xml file i.e activity_main.xml file, button like record button,stop button,play button.
At the end, i have to create simple call recording app in kotlin using MediaRecorder.
package com.example.kotlinapp
import androidx.core.app.ActivityCompat
import android.Manifest
import android.content.pm.PackageManager
import android.media.MediaPlayer
import android.media.MediaRecorder
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import java.io.IOException
private const val LOG_TAG = "AudioRecordTest"
private const val REQUEST_RECORD_AUDIO_PERMISSION = 200
class AudioRecordTest : AppCompatActivity() {
private var fileName: String = ""
private var recordButton: RecordButton? = null
private var recorder: MediaRecorder? = null
private var playButton: PlayButton? = null
private var player: MediaPlayer? = null
// Requesting permission to RECORD_AUDIO
private var permissionToRecordAccepted = false
private var permissions: Array<String> = arrayOf(Manifest.permission.RECORD_AUDIO)
override fun onRequestPermissionsResult(
requestCode: Int ,
permissions: Array<String> ,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode , permissions , grantResults)
permissionToRecordAccepted = if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) {
grantResults[0] == PackageManager.PERMISSION_GRANTED
} else {
false
}
if (!permissionToRecordAccepted) finish()
}
private fun onRecord(start: Boolean) = if (start) {
startRecording()
} else {
stopRecording()
}
private fun onPlay(start: Boolean) = if (start) {
startPlaying()
} else {
stopPlaying()
}
private fun startPlaying() {
player = MediaPlayer().apply {
try {
setDataSource(fileName)
prepare()
start()
} catch (e: IOException) {
Log.e(LOG_TAG , "prepare() failed")
}
}
}
private fun stopPlaying() {
player?.release()
player = null
}
private fun startRecording() {
recorder = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
setOutputFile(fileName)
setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
try {
prepare()
} catch (e: IOException) {
Log.e(LOG_TAG , "prepare() failed")
}
start()
}
}
private fun stopRecording() {
recorder?.apply {
stop()
release()
}
recorder = null
}
internal inner class RecordButton(ctx: AudioRecordTest) : Button(ctx) {
var mStartRecording = true
var clicker: OnClickListener = OnClickListener {
onRecord(mStartRecording)
text = when (mStartRecording) {
true -> "Stop recording"
false -> "Start recording"
}
mStartRecording = !mStartRecording
}
init {
text = "Start recording"
setOnClickListener(clicker)
}
}
internal inner class PlayButton(ctx: AudioRecordTest) : Button(ctx) {
var mStartPlaying = true
var clicker: OnClickListener = OnClickListener {
onPlay(mStartPlaying)
text = when (mStartPlaying) {
true -> "Stop playing"
false -> "Start playing"
}
mStartPlaying = !mStartPlaying
}
init {
text = "Start playing"
setOnClickListener(clicker)
}
}
override fun onCreate(icicle: Bundle?) {
super.onCreate(icicle)
// Record to the external cache directory for visibility
fileName = "${externalCacheDir?.absolutePath}/audiorecordtest.3gp"
ActivityCompat.requestPermissions(this , permissions , REQUEST_RECORD_AUDIO_PERMISSION)
recordButton = RecordButton(this)
playButton = PlayButton(this)
val ll = LinearLayout(this).apply {
addView(
recordButton ,
LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT ,
ViewGroup.LayoutParams.WRAP_CONTENT ,
0f
)
)
addView(
playButton ,
LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT ,
ViewGroup.LayoutParams.WRAP_CONTENT ,
0f
)
)
}
setContentView(ll)
}
override fun onStop() {
super.onStop()
recorder?.release()
recorder = null
player?.release()
player = null
}
}
Upvotes: 1
Views: 340
Reputation: 1125
You cannot convert the code into an xml. The code and the layout file (the xml) are complimentary. You need to create the layout containing your activity's UI (including your button) and put it into your /res folder. Here is an answered question that might point you in the right direction. When that is done, you can reference the button from your xml and execute the logic you have in your activity class. I would also strongly suggest that you a) read the basic documentation here and b) use your Android studio to create a new project with a basic Activity to study how things are connected.
Upvotes: 1