From c3d93b2b030bb95516c07ac537526625ae4d4363 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 22 Feb 2024 13:09:50 +0530 Subject: [PATCH] Rename the enum Update the code as per reviewer suggestion --- .../simform/audio_waveforms/AudioPlayer.kt | 46 ++++++++++--------- .../com/simform/audio_waveforms/Utils.kt | 2 +- example/lib/chat_bubble.dart | 2 +- ios/Classes/AudioPlayer.swift | 11 ++--- ios/Classes/Utils.swift | 2 +- lib/src/base/audio_waveforms_interface.dart | 4 +- lib/src/base/utils.dart | 40 ++++++---------- lib/src/controllers/player_controller.dart | 16 +++---- 8 files changed, 55 insertions(+), 68 deletions(-) diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt b/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt index 309b0e5c..a82db059 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt @@ -10,9 +10,9 @@ import com.google.android.exoplayer2.Player import io.flutter.plugin.common.MethodChannel class AudioPlayer( - context: Context, - channel: MethodChannel, - playerKey: String + context: Context, + channel: MethodChannel, + playerKey: String ) { private var handler: Handler = Handler(Looper.getMainLooper()) private var runnable: Runnable? = null @@ -21,15 +21,15 @@ class AudioPlayer( private var player: ExoPlayer? = null private var playerListener: Player.Listener? = null private var isPlayerPrepared: Boolean = false - private var releaseMode = ReleaseMode.Release + private var finishMode = FinishMode.Release private var key = playerKey private var updateFrequency:Long = 200 fun preparePlayer( - result: MethodChannel.Result, - path: String?, - volume: Float?, - frequency: Long?, + result: MethodChannel.Result, + path: String?, + volume: Float?, + frequency: Long?, ) { if (path != null) { frequency?.let { @@ -51,20 +51,22 @@ class AudioPlayer( } if (state == Player.STATE_ENDED) { val args: MutableMap = HashMap() - when(releaseMode){ - ReleaseMode.Release -> { + when (finishMode) { + FinishMode.Release -> { player?.stop() player?.release() player = null stopListening() args[Constants.releaseType] = 0 } - ReleaseMode.Loop -> { + + FinishMode.Loop -> { player?.seekTo(0) player?.play() args[Constants.releaseType] = 1 } - ReleaseMode.Pause -> { + + FinishMode.Pause -> { player?.seekTo(0) player?.playWhenReady = false stopListening() @@ -73,8 +75,8 @@ class AudioPlayer( } args[Constants.playerKey] = key methodChannel.invokeMethod( - Constants.onDidFinishPlayingAudio, - args + Constants.onDidFinishPlayingAudio, + args ) } } @@ -177,24 +179,26 @@ class AudioPlayer( } } - fun setReleaseMode(result: MethodChannel.Result, releaseModeType : Int?){ - try{ + fun setReleaseMode(result: MethodChannel.Result, releaseModeType: Int?) { + try { releaseModeType?.let { when (releaseModeType) { 0 -> { - this.releaseMode = ReleaseMode.Release + this.finishMode = FinishMode.Release } + 1 -> { - this.releaseMode = ReleaseMode.Loop + this.finishMode = FinishMode.Loop } + else -> { - this.releaseMode = ReleaseMode.Pause + this.finishMode = FinishMode.Pause } } } - }catch (e:Exception){ - result.error(Constants.LOG_TAG,"Can not set the release mode",e.toString()) + } catch (e: Exception) { + result.error(Constants.LOG_TAG, "Can not set the release mode", e.toString()) } } diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt b/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt index 248bb9bc..4d882c67 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt @@ -80,7 +80,7 @@ enum class UpdateFrequency(val value:Long) { Low(200), } -enum class ReleaseMode(val value: Int){ +enum class FinishMode(val value: Int){ Release(0), Loop(1), Pause(2) diff --git a/example/lib/chat_bubble.dart b/example/lib/chat_bubble.dart index c3984cbb..4d034a30 100644 --- a/example/lib/chat_bubble.dart +++ b/example/lib/chat_bubble.dart @@ -154,7 +154,7 @@ class _WaveBubbleState extends State { controller.playerState.isPlaying ? await controller.pausePlayer() : await controller.startPlayer(); - controller.setReleaseMode(ReleaseMode.release); + controller.setReleaseMode(FinishMode.loop); }, icon: Icon( controller.playerState.isPlaying diff --git a/ios/Classes/AudioPlayer.swift b/ios/Classes/AudioPlayer.swift index 7773517a..5a4b4407 100644 --- a/ios/Classes/AudioPlayer.swift +++ b/ios/Classes/AudioPlayer.swift @@ -7,7 +7,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate { private var stopWhenCompleted = false private var timer: Timer? private var player: AVAudioPlayer? - private var releaseMode:ReleaseMode = ReleaseMode.release + private var finishMode: FinishMode = FinishMode.release private var updateFrequency = 200 var plugin: SwiftAudioWaveformsPlugin var playerKey: String @@ -47,7 +47,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate { var finishType = 2 var releaseType = 0 - switch self.releaseMode{ + switch self.finishMode{ case .release: self.player?.stop() stopListening() @@ -78,7 +78,6 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate { result(true) } - func pausePlayer(result: @escaping FlutterResult) { stopListening() player?.pause() @@ -129,11 +128,11 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate { func setReleaseMode(result : @escaping FlutterResult, releaseType : Int?){ if(releaseType != nil && releaseType == 0){ - self.releaseMode = ReleaseMode.release + self.finishMode = FinishMode.release }else if(releaseType != nil && releaseType == 1){ - self.releaseMode = ReleaseMode.loop + self.finishMode = FinishMode.loop }else{ - self.releaseMode = ReleaseMode.pause + self.finishMode = FinishMode.pause } } diff --git a/ios/Classes/Utils.swift b/ios/Classes/Utils.swift index af250e5d..8016b857 100644 --- a/ios/Classes/Utils.swift +++ b/ios/Classes/Utils.swift @@ -74,7 +74,7 @@ public extension RangeReplaceableCollection where Iterator.Element: ExpressibleB } } -enum ReleaseMode : Int{ +enum FinishMode : Int{ case release = 0 case loop = 1 case pause = 2 diff --git a/lib/src/base/audio_waveforms_interface.dart b/lib/src/base/audio_waveforms_interface.dart index bc52671c..fa1b2841 100644 --- a/lib/src/base/audio_waveforms_interface.dart +++ b/lib/src/base/audio_waveforms_interface.dart @@ -175,9 +175,9 @@ class AudioWaveformsInterface { } /// Sets the release mode. - Future setReleaseMode(String key,ReleaseMode releaseMode)async{ + Future setReleaseMode(String key,FinishMode finishMode)async{ return await _methodChannel.invokeMethod(Constants.setReleaseMode,{ - Constants.releaseType : releaseMode.index, + Constants.releaseType : finishMode.index, Constants.playerKey: key, }); } diff --git a/lib/src/base/utils.dart b/lib/src/base/utils.dart index efd6f52c..06b81a82 100644 --- a/lib/src/base/utils.dart +++ b/lib/src/base/utils.dart @@ -129,14 +129,22 @@ extension FilterForPlayer on Stream> { /// An enum to be used to change behaviour of player when audio /// is finished playing. enum FinishMode { - ///Loops the audio. + ///Release all resources, just like calling release method. + /// + /// In Android, the media player is quite resource-intensive, and this will + /// let it go. Data will be buffered again when needed (if it's a remote file, + /// it will be downloaded again). + /// In iOS works just like stop method. + /// + /// This is the default behavior. + release, + + ///Keeps the buffered data and plays again after completion, creating a loop. loop, - ///Pause the audio, playing again will start from [0] milliseconds. + ///Stop audio playback but keep all resources intact. + ///Use this if you intend to play again later. pause, - - ///Stops player and disposes it(a PlayerController won't be disposed). - stop } /// An enum to decide which type of waveform to show. @@ -198,25 +206,3 @@ enum UpdateFrequency { const UpdateFrequency(this.value); final int value; } - -/// This enum is meant to be used as a parameter of setReleaseMode method. -/// -/// It represents the behavior of AudioPlayer when an audio is finished or stopped. -enum ReleaseMode{ - ///Release all resources, just like calling release method. - /// - /// In Android, the media player is quite resource-intensive, and this will - /// let it go. Data will be buffered again when needed (if it's a remote file, - /// it will be downloaded again). - /// In iOS works just like stop method. - /// - /// This is the default behavior. - release, - - ///Keeps the buffered data and plays again after completion, creating a loop. - loop, - - ///Stop audio playback but keep all resources intact. - ///Use this if you intend to play again later. - pause, -} diff --git a/lib/src/controllers/player_controller.dart b/lib/src/controllers/player_controller.dart index ee1aa542..9d0923a3 100644 --- a/lib/src/controllers/player_controller.dart +++ b/lib/src/controllers/player_controller.dart @@ -75,9 +75,9 @@ class PlayerController extends ChangeNotifier { Stream get onCompletion => PlatformStreams.instance.onCompletion.filter(playerKey); - ReleaseMode _releaseMode = ReleaseMode.release; + FinishMode _releaseMode = FinishMode.release; - ReleaseMode get releaseMode => _releaseMode; + FinishMode get releaseMode => _releaseMode; PlayerController() { if (!PlatformStreams.instance.isInitialised) { @@ -180,10 +180,8 @@ class PlayerController extends ChangeNotifier { /// A function to start the player to play/resume the audio. /// /// When playing audio is finished, this [player] will be [stopped] - /// and [disposed] by default. To change this behavior use [FinishMode] enum. + /// and [disposed] by default. To change this behavior use [setReleaseMode] Method. /// - /// See also: - /// * [FinishMode] Future startPlayer({ bool forceRefresh = true, }) async { @@ -273,10 +271,10 @@ class PlayerController extends ChangeNotifier { ///Set the release Mode. /// - /// Check[ReleaseMode]'s doc to understand the difference between the modes. - Future setReleaseMode(ReleaseMode releaseMode)async{ - _releaseMode = releaseMode; - return AudioWaveformsInterface.instance.setReleaseMode(playerKey, releaseMode); + /// Check[FinishMode]'s doc to understand the difference between the modes. + Future setReleaseMode(FinishMode finishMode)async{ + _releaseMode = finishMode; + return AudioWaveformsInterface.instance.setReleaseMode(playerKey, finishMode); } /// Release any resources taken by this controller. Disposing this