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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ build/
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

/audio_wave.iml
/audio_waveforms.iml

.idea/instapk.xml
instapk.log*
ios/Frameworks/
ios/Runner.xcworkspace/xcshareddata/
example/ios/Flutter/flutter_export_environment.sh
audio_wave.iml
audio_waveforms.iml
pubspec.lock
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions .idea/git_toolbox_prj.xml

This file was deleted.

19 changes: 0 additions & 19 deletions .idea/libraries/Dart_SDK.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/libraries/Flutter_Plugins.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/runConfigurations/example_lib_main_dart.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# audio_wave
# audio_waveforms

A new Flutter project.

Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group 'com.simform.audiowave.audio_wave'
group 'com.simform.audio_waveforms'
version '1.0-SNAPSHOT'

buildscript {
Expand Down
2 changes: 1 addition & 1 deletion android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'audio_wave'
rootProject.name = 'audio_waveforms'
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simform.audiowave.audio_wave">
package="com.simform.audio_waveforms">
</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.simform.audiowave.audio_wave
package com.simform.audio_waveforms

import android.Manifest
import android.app.Activity
Expand All @@ -14,10 +14,10 @@ import java.io.IOException
import java.lang.IllegalStateException
import kotlin.math.log10

private const val LOG_TAG = "AudioWave"
private const val LOG_TAG = "AudioWaveforms"
private const val RECORD_AUDIO_REQUEST_CODE = 1001

class AudioWaveMethodCall : PluginRegistry.RequestPermissionsResultListener {
class AudioWaveformsMethodCall : PluginRegistry.RequestPermissionsResultListener {
private var permissions = arrayOf(Manifest.permission.RECORD_AUDIO)
fun getDecibel(result: MethodChannel.Result, recorder: MediaRecorder?) {
val db = 20 * log10((recorder?.maxAmplitude?.toDouble() ?: 0.0 / 32768.0))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.simform.audio_waveforms

import android.app.Activity
import android.media.MediaRecorder
import android.os.Build
import android.util.Log
import androidx.annotation.NonNull
import androidx.annotation.RequiresApi

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
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 java.io.File
import java.io.IOException
import java.text.DateFormat.getDateTimeInstance
import java.text.SimpleDateFormat
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

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()
}

@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
try {
recorder = MediaRecorder()
} catch (e: Exception) {
Log.e(Constants.LOG_TAG, "Failed to initialise Recorder")
}

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 onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}

override fun onDetachedFromActivityForConfigChanges() {
activity = null
}

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

override fun onDetachedFromActivity() {
recorder?.release()
recorder = null
activity = null
}
}
Loading