Skip to content

Commit

Permalink
Rename the enum
Browse files Browse the repository at this point in the history
Update the code as per reviewer suggestion
  • Loading branch information
himanshuGandhiSimform committed Oct 17, 2024
1 parent 3affd63 commit c3d93b2
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 68 deletions.
46 changes: 25 additions & 21 deletions android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -51,20 +51,22 @@ class AudioPlayer(
}
if (state == Player.STATE_ENDED) {
val args: MutableMap<String, Any?> = 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()
Expand All @@ -73,8 +75,8 @@ class AudioPlayer(
}
args[Constants.playerKey] = key
methodChannel.invokeMethod(
Constants.onDidFinishPlayingAudio,
args
Constants.onDidFinishPlayingAudio,
args
)
}
}
Expand Down Expand Up @@ -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())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion example/lib/chat_bubble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class _WaveBubbleState extends State<WaveBubble> {
controller.playerState.isPlaying
? await controller.pausePlayer()
: await controller.startPlayer();
controller.setReleaseMode(ReleaseMode.release);
controller.setReleaseMode(FinishMode.loop);
},
icon: Icon(
controller.playerState.isPlaying
Expand Down
11 changes: 5 additions & 6 deletions ios/Classes/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -78,7 +78,6 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
result(true)
}


func pausePlayer(result: @escaping FlutterResult) {
stopListening()
player?.pause()
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/src/base/audio_waveforms_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ class AudioWaveformsInterface {
}

/// Sets the release mode.
Future<void> setReleaseMode(String key,ReleaseMode releaseMode)async{
Future<void> setReleaseMode(String key,FinishMode finishMode)async{
return await _methodChannel.invokeMethod(Constants.setReleaseMode,{
Constants.releaseType : releaseMode.index,
Constants.releaseType : finishMode.index,
Constants.playerKey: key,
});
}
Expand Down
40 changes: 13 additions & 27 deletions lib/src/base/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,22 @@ extension FilterForPlayer<T> on Stream<PlayerIdentifier<T>> {
/// 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.
Expand Down Expand Up @@ -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,
}
16 changes: 7 additions & 9 deletions lib/src/controllers/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ class PlayerController extends ChangeNotifier {
Stream<void> 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) {
Expand Down Expand Up @@ -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<void> startPlayer({
bool forceRefresh = true,
}) async {
Expand Down Expand Up @@ -273,10 +271,10 @@ class PlayerController extends ChangeNotifier {

///Set the release Mode.
///
/// Check[ReleaseMode]'s doc to understand the difference between the modes.
Future<void> setReleaseMode(ReleaseMode releaseMode)async{
_releaseMode = releaseMode;
return AudioWaveformsInterface.instance.setReleaseMode(playerKey, releaseMode);
/// Check[FinishMode]'s doc to understand the difference between the modes.
Future<void> setReleaseMode(FinishMode finishMode)async{
_releaseMode = finishMode;
return AudioWaveformsInterface.instance.setReleaseMode(playerKey, finishMode);
}

/// Release any resources taken by this controller. Disposing this
Expand Down

0 comments on commit c3d93b2

Please sign in to comment.