A simple audio controller for android application.
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:SimpleAudioController:0.0.1'
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
...
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>
class MainActivity : AppCompatActivity() {
private var permissions: Array<String> = arrayOf(
Manifest.permission.READ_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()
}
}
}
<view
android:id="@+id/audioController"
class="com.example.audiocontrollerlibrary.AudioController"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
This setAudioSource(filepath) method works properly only after the permission granted.
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val filepath = File(Environment.getExternalStorageDirectory(), "[Your Audio File]").absolutePath
binding.audioController.setAudioSource(filepath)
...
}
}