Skip to content

Commit

Permalink
do not allocate large byte buffers for audio. no visualizer if audio …
Browse files Browse the repository at this point in the history
…file is too large
  • Loading branch information
zoff99 committed Sep 20, 2024
1 parent e1ad2ca commit 28bf58c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

public class FileUtils {

public static final int MAX_FILE_SIZE_BYTES = 500000; // ~500 kByte

public static void updateVisualizer(final Context context, final File file, final PlayerVisualizerSeekbar playerVisualizerSeekbar){
Log.e(" BYTES", "CALLED");
new AsyncTask<Void, Void, byte[]>() {
Expand All @@ -24,8 +26,6 @@ protected byte[] doInBackground(Void... voids) {
@Override
protected void onPostExecute(final byte[] bytes) {
super.onPostExecute(bytes);
Log.e("BYTES", String.valueOf(bytes.length));

((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
Expand All @@ -37,6 +37,10 @@ public void run() {
}.execute();
}
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;

import com.zoffcc.applications.trifa.R;

Expand Down Expand Up @@ -104,9 +105,21 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bytes == null || width == 0) {
if (width == 0) {
return;
}

if (bytes == null) {
float left = 0;
float middle_vertical_top = (height / 2) - 1;
float middle_vertical_bottom = (height / 2) + 1;
float right = width;
canvas.drawRect(left, middle_vertical_top, right, middle_vertical_bottom, playedStatePainting);
canvas.drawRect(left, middle_vertical_top, denseness, middle_vertical_bottom, notPlayedStatePainting);
canvas.drawCircle(denseness, (height / 2), dp(6), notPlayedStatePainting);
return;
}

float totalBarsCount = width / dp(3);
if (totalBarsCount <= 0.1f) {
return;
Expand All @@ -117,7 +130,7 @@ protected void onDraw(Canvas canvas) {
float barCounter = 0;
int nextBarNum = 0;

int y = (height - dp(VISUALIZER_HEIGHT)) ;
int y = (height - dp(VISUALIZER_HEIGHT));
int barNum = 0;
int lastBarNum;
int drawBarCount;
Expand Down

0 comments on commit 28bf58c

Please sign in to comment.