@@ -7,7 +7,6 @@ import android.os.Build
77import android.util.Log
88import androidx.annotation.NonNull
99import androidx.annotation.RequiresApi
10-
1110import io.flutter.embedding.engine.plugins.FlutterPlugin
1211import io.flutter.embedding.engine.plugins.activity.ActivityAware
1312import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
@@ -18,7 +17,8 @@ import io.flutter.plugin.common.MethodChannel.Result
1817import java.io.File
1918import java.io.IOException
2019import java.text.SimpleDateFormat
21- import java.util.*
20+ import java.util.Date
21+ import java.util.Locale
2222
2323
2424/* * AudioWaveformsPlugin */
@@ -55,13 +55,17 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
5555 recorderSettings
5656 )
5757 } else {
58- result.error(Constants .LOG_TAG , " Failed to initialise Recorder" , " Invalid Arguments" )
58+ result.error(
59+ Constants .LOG_TAG ,
60+ " Failed to initialise Recorder" ,
61+ " Invalid Arguments"
62+ )
5963 }
6064 }
6165
6266 Constants .startRecording -> {
6367 val useLegacyNormalization =
64- (call.argument(Constants .useLegacyNormalization) as Boolean? ) ? : false
68+ (call.argument(Constants .useLegacyNormalization) as Boolean? ) ? : false
6569 audioRecorder.startRecorder(result, recorder, useLegacyNormalization)
6670 }
6771
@@ -77,7 +81,12 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
7781 Constants .pauseRecording -> audioRecorder.pauseRecording(result, recorder)
7882 Constants .resumeRecording -> audioRecorder.resumeRecording(result, recorder)
7983 Constants .getDecibel -> audioRecorder.getDecibel(result, recorder)
80- Constants .checkPermission -> audioRecorder.checkPermission(result, activity, result::success)
84+ Constants .checkPermission -> audioRecorder.checkPermission(
85+ result,
86+ activity,
87+ result::success
88+ )
89+
8190 Constants .preparePlayer -> {
8291 val audioPath = call.argument(Constants .path) as String?
8392 val volume = call.argument(Constants .volume) as Double?
@@ -109,7 +118,12 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
109118 Constants .stopPlayer -> {
110119 val key = call.argument(Constants .playerKey) as String?
111120 if (key != null ) {
112- audioPlayers[key]?.stop(result)
121+ try {
122+ audioPlayers[key]?.stop()
123+ result.success(true )
124+ } catch (e: Exception ) {
125+ result.error(Constants .LOG_TAG , " Failed to stop player" , e.message)
126+ }
113127 } else {
114128 result.error(Constants .LOG_TAG , " Player key can't be null" , " " )
115129 }
@@ -118,7 +132,12 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
118132 Constants .pausePlayer -> {
119133 val key = call.argument(Constants .playerKey) as String?
120134 if (key != null ) {
121- audioPlayers[key]?.pause(result)
135+ try {
136+ audioPlayers[key]?.pause()
137+ result.success(true )
138+ } catch (e: Exception ) {
139+ result.error(Constants .LOG_TAG , " Failed to pause player" , e.message)
140+ }
122141 } else {
123142 result.error(Constants .LOG_TAG , " Player key can't be null" , " " )
124143 }
@@ -140,8 +159,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
140159 }
141160 } else {
142161 Log .e(
143- Constants .LOG_TAG ,
144- " Minimum android O is required for seekTo function to works"
162+ Constants .LOG_TAG ,
163+ " Minimum android O is required for seekTo function to works"
145164 )
146165 }
147166 }
@@ -168,7 +187,7 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
168187
169188 Constants .getDuration -> {
170189 val type =
171- if ((call.argument(Constants .durationType) as Int? ) == 0 ) DurationType .Current else DurationType .Max
190+ if ((call.argument(Constants .durationType) as Int? ) == 0 ) DurationType .Current else DurationType .Max
172191 val key = call.argument(Constants .playerKey) as String?
173192 if (key != null ) {
174193 audioPlayers[key]?.getDuration(result, type)
@@ -183,22 +202,18 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
183202 val noOfSample = call.argument(Constants .noOfSamples) as Int?
184203 if (key != null ) {
185204 createOrUpdateExtractor(
186- playerKey = key,
187- result = result,
188- path = path,
189- noOfSamples = noOfSample ? : 100 ,
205+ playerKey = key,
206+ result = result,
207+ path = path,
208+ noOfSamples = noOfSample ? : 100 ,
190209 )
191210 } else {
192211 result.error(Constants .LOG_TAG , " Player key can't be null" , " " )
193212 }
194213 }
195214
196215 Constants .stopAllPlayers -> {
197- for ((key, _) in audioPlayers) {
198- audioPlayers[key]?.stop(result)
199- audioPlayers[key] = null
200- }
201- result.success(true )
216+ stopAllPlayer(result)
202217 }
203218
204219 Constants .finishMode -> {
@@ -209,6 +224,10 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
209224 }
210225 }
211226
227+ Constants .pauseAllPlayers -> {
228+ pauseAllPlayer(result)
229+ }
230+
212231 else -> result.notImplemented()
213232 }
214233 }
@@ -251,40 +270,40 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
251270 private fun initPlayer (playerKey : String ) {
252271 if (audioPlayers[playerKey] == null ) {
253272 val newPlayer = AudioPlayer (
254- context = applicationContext,
255- channel = channel,
256- playerKey = playerKey,
273+ context = applicationContext,
274+ channel = channel,
275+ playerKey = playerKey,
257276 )
258277 audioPlayers[playerKey] = newPlayer
259278 }
260279 return
261280 }
262281
263282 private fun createOrUpdateExtractor (
264- playerKey : String ,
265- noOfSamples : Int ,
266- path : String? ,
267- result : Result ,
283+ playerKey : String ,
284+ noOfSamples : Int ,
285+ path : String? ,
286+ result : Result ,
268287 ) {
269288 if (path == null ) {
270289 result.error(Constants .LOG_TAG , " Path can't be null" , " " )
271290 return
272291 }
273292 extractors[playerKey] = WaveformExtractor (
274- context = applicationContext,
275- methodChannel = channel,
276- expectedPoints = noOfSamples,
277- key = playerKey,
278- path = path,
279- result = result,
280- extractorCallBack = object : ExtractorCallBack {
281- override fun onProgress (value : Float ) {
282- if (value == 1.0F ) {
283- result.success(extractors[playerKey]?.sampleData)
284- }
293+ context = applicationContext,
294+ methodChannel = channel,
295+ expectedPoints = noOfSamples,
296+ key = playerKey,
297+ path = path,
298+ result = result,
299+ extractorCallBack = object : ExtractorCallBack {
300+ override fun onProgress (value : Float ) {
301+ if (value == 1.0F ) {
302+ result.success(extractors[playerKey]?.sampleData)
285303 }
286-
287304 }
305+
306+ }
288307 )
289308 extractors[playerKey]?.startDecode()
290309 extractors[playerKey]?.stop()
@@ -319,4 +338,27 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
319338 pluginBinding!! .removeRequestPermissionsResultListener(this .audioRecorder)
320339 }
321340 }
341+
342+ private fun stopAllPlayer (result : MethodChannel .Result ) {
343+ try {
344+ for ((key, _) in audioPlayers) {
345+ audioPlayers[key]?.stop()
346+ audioPlayers[key] = null
347+ }
348+ result.success(true )
349+ } catch (e: Exception ) {
350+ result.error(Constants .LOG_TAG , " Failed to stop players" , e.message)
351+ }
352+ }
353+
354+ private fun pauseAllPlayer (result : MethodChannel .Result ) {
355+ try {
356+ for ((key, _) in audioPlayers) {
357+ audioPlayers[key]?.pause()
358+ }
359+ result.success(true )
360+ } catch (e: Exception ) {
361+ result.error(Constants .LOG_TAG , " Failed to pause players" , e.message)
362+ }
363+ }
322364}
0 commit comments