Skip to content
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,120 +22,119 @@ import java.util.*

/** AudioWaveformsPlugin */
class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
private lateinit var channel: MethodChannel
private var recorder: MediaRecorder? = null
private var activity: Activity? = null
private lateinit var audioWaveMethodCall: AudioWaveformsMethodCall
private var path: String? = null
private var codec: Int = 0
private var sampleRate: Int = 16000
private lateinit var channel: MethodChannel
private var recorder: MediaRecorder? = null
private var activity: Activity? = null
private lateinit var audioWaveMethodCall: AudioWaveformsMethodCall
private var path: String? = null
private var codec: Int = 0
private var sampleRate: Int = 16000

object Constants {
const val initRecorder = "initRecorder"
const val startRecording = "startRecording"
const val stopRecording = "stopRecording"
const val pauseRecording = "pauseRecording"
const val resumeRecording = "resumeRecording"
const val getDecibel = "getDecibel"
const val checkPermission = "checkPermission"
const val path = "path"
const val LOG_TAG = "AudioWave"
const val methodChannelName = "simform_audio_waveforms_plugin/methods"
const val enCoder = "enCoder"
const val sampleRate = "sampleRate"
const val fileNameFormat = "dd-MM-yy-hh-mm-ss"
}
object Constants {
const val initRecorder = "initRecorder"
const val startRecording = "startRecording"
const val stopRecording = "stopRecording"
const val pauseRecording = "pauseRecording"
const val resumeRecording = "resumeRecording"
const val getDecibel = "getDecibel"
const val checkPermission = "checkPermission"
const val path = "path"
const val LOG_TAG = "AudioWave"
const val methodChannelName = "simform_audio_waveforms_plugin/methods"
const val enCoder = "enCoder"
const val sampleRate = "sampleRate"
const val fileNameFormat = "dd-MM-yy-hh-mm-ss"
}

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, Constants.methodChannelName)
channel.setMethodCallHandler(this)
audioWaveMethodCall = AudioWaveformsMethodCall()
}
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, Constants.methodChannelName)
channel.setMethodCallHandler(this)
audioWaveMethodCall = AudioWaveformsMethodCall()
}

@RequiresApi(Build.VERSION_CODES.N)
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
Constants.initRecorder -> {
path = call.argument(Constants.path) as String?
codec = (call.argument(Constants.enCoder) as Int?) ?: 0
sampleRate = (call.argument(Constants.sampleRate) as Int?) ?: 16000
checkPathAndInitialiseRecorder(result, codec, sampleRate)
}
Constants.startRecording -> audioWaveMethodCall.startRecorder(result, recorder)
Constants.stopRecording -> {
audioWaveMethodCall.stopRecording(result, recorder, path!!)
recorder = null
}
Constants.pauseRecording -> audioWaveMethodCall.pauseRecording(result, recorder)
Constants.resumeRecording -> audioWaveMethodCall.resumeRecording(result, recorder)
Constants.getDecibel -> audioWaveMethodCall.getDecibel(result, recorder)
Constants.checkPermission -> audioWaveMethodCall.checkPermission(result, activity)
else -> result.notImplemented()
@RequiresApi(Build.VERSION_CODES.N)
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
Constants.initRecorder -> {
path = call.argument(Constants.path) as String?
codec = (call.argument(Constants.enCoder) as Int?) ?: 0
sampleRate = (call.argument(Constants.sampleRate) as Int?) ?: 16000
checkPathAndInitialiseRecorder(result, codec, sampleRate)
}
Constants.startRecording -> audioWaveMethodCall.startRecorder(result, recorder)
Constants.stopRecording -> {
audioWaveMethodCall.stopRecording(result, recorder, path!!)
recorder = null
}
Constants.pauseRecording -> audioWaveMethodCall.pauseRecording(result, recorder)
Constants.resumeRecording -> audioWaveMethodCall.resumeRecording(result, recorder)
Constants.getDecibel -> audioWaveMethodCall.getDecibel(result, recorder)
Constants.checkPermission -> audioWaveMethodCall.checkPermission(result, activity)
else -> result.notImplemented()
}
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun checkPathAndInitialiseRecorder(
result: Result,
enCoder: Int,
sampleRate: Int
) {
if (path == null) {
val outputDir = activity?.cacheDir
val outputFile: File?
val dateTimeInstance = SimpleDateFormat(Constants.fileNameFormat,Locale.US)
val currentDate = dateTimeInstance.format(Date())
try {
outputFile = File.createTempFile(currentDate, ".aac", outputDir)
path = outputFile.path
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun checkPathAndInitialiseRecorder(
result: Result,
enCoder: Int,
sampleRate: Int
) {
try {
recorder = MediaRecorder()
recorder = MediaRecorder()
} catch (e: Exception) {
Log.e(Constants.LOG_TAG, "Failed to initialise Recorder")
Log.e(Constants.LOG_TAG, "Failed to initialise Recorder")
}
if (path == null) {
val outputDir = activity?.cacheDir
val outputFile: File?
val dateTimeInstance = SimpleDateFormat(Constants.fileNameFormat, Locale.US)
val currentDate = dateTimeInstance.format(Date())
try {
outputFile = File.createTempFile(currentDate, ".aac", outputDir)
path = outputFile.path
audioWaveMethodCall.initRecorder(
path!!,
result,
recorder,
enCoder,
enCoder,
sampleRate
)
} catch (e: IOException) {
Log.e(Constants.LOG_TAG, "Failed to create file")
}
} else {
audioWaveMethodCall.initRecorder(
path!!,
result,
recorder,
enCoder,
enCoder,
sampleRate
)
}

audioWaveMethodCall.initRecorder(
path!!,
result,
recorder,
enCoder,
enCoder,
sampleRate
)
} catch (e: IOException) {
Log.e(Constants.LOG_TAG, "Failed to create file")
}
} else {
audioWaveMethodCall.initRecorder(
path!!,
result,
recorder,
enCoder,
enCoder,
sampleRate
)
}
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}

override fun onDetachedFromActivityForConfigChanges() {
activity = null
}
override fun onDetachedFromActivityForConfigChanges() {
activity = null
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = binding.activity
}

override fun onDetachedFromActivity() {
recorder?.release()
recorder = null
activity = null
}
override fun onDetachedFromActivity() {
recorder?.release()
recorder = null
activity = null
}
}
168 changes: 0 additions & 168 deletions lib/src/audio_wave.dart

This file was deleted.