From 87288e72379106a578b2f777bf05aec5a12f8515 Mon Sep 17 00:00:00 2001 From: zoff99 Date: Sat, 21 Sep 2024 08:06:52 +0200 Subject: [PATCH] audio messages tweaks --- .../chatvoiceplayerlibrary/FileUtils.java | 26 ++++++++++++------- .../PlayerVisualizerSeekbar.java | 5 ++-- .../VoicePlayerView.java | 6 ++--- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/FileUtils.java b/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/FileUtils.java index 719654c6..7050e45c 100644 --- a/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/FileUtils.java +++ b/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/FileUtils.java @@ -16,8 +16,8 @@ public class FileUtils { public static final int MAX_FILE_SIZE_BYTES = 100000; // ~100 kByte public static void updateVisualizer(final Context context, final File file, final PlayerVisualizerSeekbar playerVisualizerSeekbar){ - Log.e(" BYTES", "CALLED"); - new AsyncTask() { + new AsyncTask() + { @Override protected byte[] doInBackground(Void... voids) { return fileToBytes(file); @@ -28,7 +28,8 @@ protected void onPostExecute(final byte[] bytes) { super.onPostExecute(bytes); ((Activity) context).runOnUiThread(new Runnable() { @Override - public void run() { + public void run() + { playerVisualizerSeekbar.setBytes(bytes); playerVisualizerSeekbar.invalidate(); } @@ -36,21 +37,26 @@ public void run() { } }.execute(); } - public static byte[] fileToBytes(File file) { + + public static byte[] fileToBytes(File file) + { if (file.length() > MAX_FILE_SIZE_BYTES) { return null; } - int size = (int) file.length(); - byte[] bytes = new byte[size]; - try { + byte[] bytes = null; + try + { + int size = (int) file.length(); + bytes = new byte[size]; BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file)); buf.read(bytes, 0, bytes.length); buf.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { + } + catch (Exception e) + { e.printStackTrace(); + return null; } return bytes; } diff --git a/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/PlayerVisualizerSeekbar.java b/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/PlayerVisualizerSeekbar.java index 5efc7fad..5055abc5 100644 --- a/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/PlayerVisualizerSeekbar.java +++ b/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/PlayerVisualizerSeekbar.java @@ -34,11 +34,11 @@ public class PlayerVisualizerSeekbar extends androidx.appcompat.widget.AppCompat /** * Canvas painting for sample scale, filling played part of audio sample */ - private Paint playedStatePainting = new Paint(); + private final Paint playedStatePainting = new Paint(); /** * Canvas painting for sample scale, filling not played part of audio sample */ - private Paint notPlayedStatePainting = new Paint(); + private final Paint notPlayedStatePainting = new Paint(); private int width; private int height; @@ -76,6 +76,7 @@ public void updateVisualizer(File file) { FileUtils.updateVisualizer(getContext(), file, this); } + public void setBytes(byte[] bytes){ this.bytes = bytes; } diff --git a/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/VoicePlayerView.java b/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/VoicePlayerView.java index 6c3a0179..de1d73b0 100644 --- a/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/VoicePlayerView.java +++ b/android-refimpl-app/app/src/main/java/me/jagar/chatvoiceplayerlibrary/VoicePlayerView.java @@ -213,7 +213,7 @@ public void onCompletion(MediaPlayer mp) { seekbarV.updateVisualizer(new File(path)); } seekbarV.setOnSeekBarChangeListener(seekBarListener); - seekbarV.updateVisualizer(new File(path)); + // seekbarV.updateVisualizer(new File(path)); } @@ -634,10 +634,10 @@ public void run() { imgPause.setOnClickListener(imgPauseClickListener); imgShare.setOnClickListener(imgShareClickListener); if (seekbarV.getVisibility() == VISIBLE){ - seekbarV.updateVisualizer(new File(path)); - Log.i("VoicePlayerView", "VoicePlayerView:refreshPlayer:updateVisualizer"); + // seekbarV.updateVisualizer(new File(path)); seekbarV.setOnSeekBarChangeListener(seekBarListener); seekbarV.updateVisualizer(new File(path)); + Log.i("VoicePlayerView", "VoicePlayerView:refreshPlayer:updateVisualizer"); } } });