From d08dc3cdeb191bcb0dc76ad96230d183ce98c53a Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 28 Nov 2024 15:00:36 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Cannot=20catch=20error=20?= =?UTF-8?q?of=20preparePlayer=20(#301)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../simform/audio_waveforms/AudioPlayer.kt | 31 ++++++++++++------- .../audio_waveforms/AudioWaveformsPlugin.kt | 2 +- example/pubspec.yaml | 4 ++- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3352b560..127a24ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fixed [#304](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/304) - Updated Documentation. - Fixed [#349](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/349) - iOS audio plays without sound - Fixed seekTo position issue where onDrag of waveform at initial position first wave outside the seekLine +- Fixed [#301](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/301) - Cannot catch error of preparePlayer ## 1.1.1 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 69f49a0b..f90b1f90 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt @@ -6,14 +6,14 @@ import android.os.Handler import android.os.Looper import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem +import com.google.android.exoplayer2.PlaybackException import com.google.android.exoplayer2.Player import io.flutter.plugin.common.MethodChannel -import java.lang.Exception 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 @@ -24,13 +24,13 @@ class AudioPlayer( private var isPlayerPrepared: Boolean = false private var finishMode = FinishMode.Stop private var key = playerKey - private var updateFrequency:Long = 200 + 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 { @@ -42,6 +42,12 @@ class AudioPlayer( player?.addMediaItem(mediaItem) player?.prepare() playerListener = object : Player.Listener { + + override fun onPlayerError(error: PlaybackException) { + super.onPlayerError(error) + result.error(Constants.LOG_TAG, error.message, "Unable to load media source.") + } + override fun onPlayerStateChanged(isReady: Boolean, state: Int) { if (!isPlayerPrepared) { if (state == Player.STATE_READY) { @@ -58,12 +64,14 @@ class AudioPlayer( player?.play() args[Constants.finishType] = 0 } + FinishMode.Pause -> { player?.seekTo(0) player?.playWhenReady = false stopListening() args[Constants.finishType] = 1 } + else -> { player?.stop() player?.release() @@ -74,8 +82,8 @@ class AudioPlayer( } args[Constants.playerKey] = key methodChannel.invokeMethod( - Constants.onDidFinishPlayingAudio, - args + Constants.onDidFinishPlayingAudio, + args ) } } @@ -149,6 +157,7 @@ class AudioPlayer( } } + fun release(result: MethodChannel.Result) { try { player?.release() diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt b/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt index 3f783fd9..1b80c40b 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt @@ -252,7 +252,7 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { result: Result, ) { if (path == null) { - result.error(Constants.LOG_TAG, "Path cant be null", "") + result.error(Constants.LOG_TAG, "Path can't be null", "") return } extractors[playerKey] = WaveformExtractor( diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 6aef9e8f..43ff99fe 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -32,6 +32,9 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.5 +dependency_overrides: + win32: ^5.7.2 + dev_dependencies: flutter_test: sdk: flutter @@ -61,7 +64,6 @@ flutter: - assets/audios/audio2.mp3 - assets/audios/audio3.mp3 - assets/audios/audio4.mp3 - # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware.