A library for audio recording at foreground.
To get this library into your build:
Add it in your settings.gradle at the end of repositories:
repositories {
...
maven { url 'https://jitpack.io' }
}
Add it in your build.gradle (:app):
dependencies {
...
implementation 'com.github.HyunJinNo:ForegroundAudioRecorder:0.0:3'
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
class MainActivity : AppCompatActivity() {
private var permissions: Array<String> = arrayOf(
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
companion object {
private const val REQUEST_PERMISSIONS = 200
}
override fun onCreate(savedInstanceState: Bundle?) {
...
requestPermissions(permissions, REQUEST_PERMISSIONS)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
val flag = if (requestCode == REQUEST_PERMISSIONS) {
grantResults.all { it == PackageManager.PERMISSION_GRANTED }
} else {
false
}
if (!flag) {
Toast.makeText(applicationContext, "Permissions rejected.", Toast.LENGTH_SHORT).show()
finish()
}
}
}
val intent = Intent(applicationContext, AudioService::class.java)
startService(intent)
If you call stopService() method during audio recording, a recorded audio file is automatically created.
val intent = Intent(applicationContext, AudioService::class.java)
stopService(intent)