Android Studio中获取音频轨道频率的技术方案求助
Hey there, I’ve been where you are—struggling to pull audio frequencies in Android after messing with FFT and visualization libraries without luck. Let’s break down the best tools and implementations to get you up and running.
Recommended Libraries & Tools
1. TarsosDSP
This is my go-to for audio signal processing on Android. It wraps FFT, pitch detection algorithms (like YIN, which works great for voice or single tones), and other audio utilities so you don’t have to reinvent the wheel. It’s lightweight and straightforward to integrate.
2. Native Android AudioRecord + Custom FFT
If you prefer avoiding third-party dependencies, you can use Android’s built-in AudioRecord to capture raw PCM audio, then run an FFT on the data to extract frequencies. This gives you full control, but you’ll need a solid FFT implementation (skip writing one from scratch—use a tested Cooley-Tukey based class).
Step-by-Step with TarsosDSP
First, add the dependency to your module-level build.gradle:
implementation 'be.tarsos.dsp:TarsosDSP:2.4.0'
Then, here’s a working code snippet to capture and detect frequencies:
import be.tarsos.dsp.AudioDispatcher; import be.tarsos.dsp.AudioEvent; import be.tarsos.dsp.AudioProcessor; import be.tarsos.dsp.io.android.AndroidAudioInputStream; import be.tarsos.dsp.pitch.PitchDetectionHandler; import be.tarsos.dsp.pitch.PitchDetectionResult; import be.tarsos.dsp.pitch.PitchProcessor; // Set up audio parameters int sampleRate = 44100; int bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); // Create an audio dispatcher to capture from the microphone AudioDispatcher dispatcher = AudioDispatcher.fromDefaultMicrophone(sampleRate, bufferSize, 0); // Handle pitch detection results PitchDetectionHandler pitchHandler = new PitchDetectionHandler() { @Override public void handlePitch(PitchDetectionResult result, AudioEvent audioEvent) { float detectedFrequency = result.getPitch(); if (detectedFrequency != -1) { // Update UI on the main thread (critical for Android!) runOnUiThread(() -> { yourFrequencyTextView.setText(String.format("Current Frequency: %.2f Hz", detectedFrequency)); }); } } }; // Use the YIN algorithm for reliable pitch detection AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.YIN, sampleRate, bufferSize, pitchHandler); dispatcher.addAudioProcessor(pitchProcessor); // Start the audio processing in a background thread new Thread(dispatcher).start();
Native Implementation Basics
If you want to go the no-library route:
- Use
AudioRecordto capture 16-bit mono PCM audio at 44100 Hz (standard for audio processing) - Pass the captured short array to an FFT implementation to convert to frequency domain data
- Find the peak energy in the FFT output—calculate the frequency with:
frequency = (peakIndex * sampleRate) / bufferSize - Pro tip: Grab a pre-written FFT class (search for "Android FFT Cooley-Tukey") to avoid math errors.
Common Pitfalls to Avoid
- Permissions: Don’t forget to add
<uses-permission android:name="android.permission.RECORD_AUDIO"/>to your manifest, and request it dynamically for Android 6.0+. - Buffer Size: Use
AudioRecord.getMinBufferSize()as a starting point, but you might need to increase it to avoid glitches. - Thread Safety: Audio processing runs in a background thread—always switch to the main thread when updating UI elements.
- Complex Audio: If you’re detecting frequencies in music (not just single tones), basic FFT peak detection might not be enough. Use algorithms like YIN or harmonic product spectrum for better accuracy.
内容的提问来源于stack exchange,提问作者Richard

