Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 Cannot catch error of preparePlayer (#301) #368

Merged
merged 1 commit into from
Dec 2, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 20 additions & 11 deletions android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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()
Expand All @@ -74,8 +82,8 @@ class AudioPlayer(
}
args[Constants.playerKey] = key
methodChannel.invokeMethod(
Constants.onDidFinishPlayingAudio,
args
Constants.onDidFinishPlayingAudio,
args
)
}
}
Expand Down Expand Up @@ -149,6 +157,7 @@ class AudioPlayer(
}

}

fun release(result: MethodChannel.Result) {
try {
player?.release()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down