I'm making an Android Audio Recording app and I managed to make it with a waveform however I'm having a hard time to make a formula for the spike even when the surroundings is very quiet I'm getting a big spike in my waveform
Result
WaveformView Class
Код: Выделить всё
public class WaveformView extends View { private Paint paint = new Paint(); private List amplitudes = new ArrayList(); private List spikes = new ArrayList(); private float radius = 6f; private float w = 9f; private float d = 6f; private float sw = 0f; private float sh = 400f; private int maxSpikes = 0; public WaveformView(Context context, AttributeSet attrs) { super(context, attrs); paint.setColor(Color.rgb(244, 81, 30)); sw = getResources().getDisplayMetrics().widthPixels; maxSpikes = (int) (sw / (w + d)); } public void addAmplitude(float amp) { float norm = Math.min((int) (amp / 7), 400); amplitudes.add(norm); spikes.clear(); List amps = new ArrayList(amplitudes.subList(Math.max(amplitudes.size() - maxSpikes, 0) , amplitudes.size())); for (int i = 0; i < amps.size(); i++) { float left = sw - i * (w + d); float top = sh/2 - amps.get(i)/2; float right = left + w; float bottom = top + amps.get(i); spikes.add(new RectF(right, top, left, bottom)); } invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (RectF spike : spikes) { canvas.drawRoundRect(spike, radius, radius, paint); } }
Код: Выделить всё
updateWaveformRunnable = new Runnable() { @Override public void run() { // Check if mediaRecorder is still recording if (isRecording) { // Ensure mediaRecorder is not null before getting max amplitude if (mediaRecorder != null) { float maxAmplitudeFloat = (float) mediaRecorder.getMaxAmplitude(); Log.d("Max Amplitude", String.valueOf(maxAmplitudeFloat)); waveformView.addAmplitude(maxAmplitudeFloat); } else { Log.e("MicFragment", "MediaRecorder is null"); } // Schedule the next update after a delay timerHandler.postDelayed(this, 100); // Adjust the interval as needed } } };
Источник: https://stackoverflow.com/questions/781 ... orm-spikes