Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugFix - Android recording doesn't work the first time after granting… #265

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private const val RECORD_AUDIO_REQUEST_CODE = 1001
class AudioRecorder : PluginRegistry.RequestPermissionsResultListener {
private var permissions = arrayOf(Manifest.permission.RECORD_AUDIO)
private var useLegacyNormalization = false
private var successCallback: RequestPermissionsSuccessCallback? = null

fun getDecibel(result: MethodChannel.Result, recorder: MediaRecorder?) {
if (useLegacyNormalization) {
Expand Down Expand Up @@ -130,6 +131,7 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener {
grantResults: IntArray
): Boolean {
return if (requestCode == RECORD_AUDIO_REQUEST_CODE) {
successCallback?.onSuccess(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
ujas-m-simformsolutions marked this conversation as resolved.
Show resolved Hide resolved
grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
} else {
false
Expand All @@ -142,7 +144,8 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener {
return result == PackageManager.PERMISSION_GRANTED
}

fun checkPermission(result: MethodChannel.Result, activity: Activity?) {
fun checkPermission(result: MethodChannel.Result, activity: Activity?, successCallback: RequestPermissionsSuccessCallback) {
this.successCallback = successCallback
ujas-m-simformsolutions marked this conversation as resolved.
Show resolved Hide resolved
if (!isPermissionGranted(activity)) {
activity?.let {
ActivityCompat.requestPermissions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
Expand All @@ -35,6 +36,7 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
private lateinit var applicationContext: Context
private var audioPlayers = mutableMapOf<String, AudioPlayer?>()
private var extractors = mutableMapOf<String, WaveformExtractor?>()
private var pluginBinding: ActivityPluginBinding? = null

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, Constants.methodChannelName)
Expand Down Expand Up @@ -66,7 +68,7 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
Constants.pauseRecording -> audioRecorder.pauseRecording(result, recorder)
Constants.resumeRecording -> audioRecorder.resumeRecording(result, recorder)
Constants.getDecibel -> audioRecorder.getDecibel(result, recorder)
Constants.checkPermission -> audioRecorder.checkPermission(result, activity)
Constants.checkPermission -> audioRecorder.checkPermission(result, activity, result :: success)
Constants.preparePlayer -> {
val audioPath = call.argument(Constants.path) as String?
val volume = call.argument(Constants.volume) as Double?
Expand Down Expand Up @@ -276,6 +278,9 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
pluginBinding = binding
pluginBinding!!.addRequestPermissionsResultListener(this.audioRecorder)

}

override fun onDetachedFromActivityForConfigChanges() {
Expand All @@ -292,5 +297,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
audioPlayers.clear()
extractors.clear()
activity = null
if (pluginBinding != null) {
pluginBinding!!.removeRequestPermissionsResultListener(this.audioRecorder)
}
}
}
4 changes: 4 additions & 0 deletions android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ enum class UpdateFrequency(val value:Long) {
High(50),
Medium(100),
Low(200),
}

fun interface RequestPermissionsSuccessCallback {
fun onSuccess(results: Boolean?)
}