English translation
Android Audio Processing: Fundamentals and Best Practices
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
In the previous chapter, we explored image processing—such as loading, editing, and saving images—in depth. Next, we turn our attention to audio processing: learning how to play, record, and process audio within Android applications. Audio processing is essential across many application domains—especially in music players, voice recording apps, and games.
1. Audio Playback
In Android, audio playback is commonly handled using the MediaPlayer class, which provides a rich set of APIs for managing audio playback. Below is a basic example of audio playback.
Example: Simple Audio Playback
Assume we want to play an audio file named sound.mp3, stored in the res/raw directory.
import android.media.MediaPlayer;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class AudioActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
// Initialize MediaPlayer
mediaPlayer = MediaPlayer.create(this, R.raw.sound);
}
@Override
protected void onDestroy() {
// Release resources
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
super.onDestroy();
}
// Play audio
public void playAudio() {
if (mediaPlayer != null) {
mediaPlayer.start();
}
}
// Pause audio
public void pauseAudio() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
// Stop audio
public void stopAudio() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.prepareAsync(); // Prepare for next playback
}
}
}
In this example, we create a MediaPlayer instance using MediaPlayer.create(). We then control playback using methods such as start(), pause(), and stop().
2. Audio Recording
Beyond playback, audio recording is another common requirement. Android provides the AudioRecord class for low-level audio capture. Below is a simple audio recording example.
Example: Simple Audio Recording
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import androidx.appcompat.app.AppCompatActivity;
import java.io.FileOutputStream;
import java.io.IOException;
public class RecordActivity extends AppCompatActivity {
private static final int RECORDER_AUDIO_SOURCE = MediaRecorder.AudioSource.MIC;
private static final int RECORDER_OUTPUT_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_SAMPLERATE = 44100;
private static final String FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/recorded_audio.pcm";
private AudioRecord audioRecord;
private boolean isRecording = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
}
public void startRecording() {
audioRecord = new AudioRecord(RECORDER_AUDIO_SOURCE, RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_OUTPUT_FORMAT, AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_OUTPUT_FORMAT));
audioRecord.startRecording();
isRecording = true;
new Thread(new Runnable() {
@Override
public void run() {
writeAudioData();
}
}).start();
}
private void writeAudioData() {
byte[] data = new byte[1024];
try (FileOutputStream os = new FileOutputStream(FILE_PATH)) {
while (isRecording) {
int read = audioRecord.read(data, 0, data.length);
if (read > 0) {
os.write(data, 0, read);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void stopRecording() {
if (audioRecord != null) {
isRecording = false;
audioRecord.stop();
audioRecord.release();
audioRecord = null;
}
}
}
In this example, AudioRecord captures raw audio data. The recording runs on a separate thread to avoid blocking the main UI thread. The captured audio is saved in PCM format to the specified file path.
3. Audio Effects Processing
For applying real-time audio effects, Android offers built-in classes such as AudioEffect. Common effects include reverb, equalization, and more. Below is a simple example demonstrating how to apply an equalizer effect.
Example: Adding an Equalizer Effect
import android.media.audiofx.Equalizer;
import android.media.MediaPlayer;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class EqualizerActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private Equalizer equalizer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_equalizer);
mediaPlayer = MediaPlayer.create(this, R.raw.sound);
equalizer = new Equalizer(0, mediaPlayer.getAudioSessionId());
equalizer.setEnabled(true);
// Configure equalizer bands
for (short i = 0; i < equalizer.getNumberOfBands(); i++) {
equalizer.setBandLevel(i, (short) (equalizer.getBandLevelRange()[1] / 2));
}
}
@Override
protected void onDestroy() {
if (equalizer != null) {
equalizer.release();
}
if (mediaPlayer != null) {
mediaPlayer.release();
}
super.onDestroy();
}
}
Here, we instantiate an Equalizer tied to the MediaPlayer’s audio session and enable it. Then, we set each frequency band to its mid-range gain level—enhancing tonal balance and overall listening experience, especially useful in music applications.
4. Summary
In this chapter, we covered three core aspects of audio handling in Android:
- Playing audio using
MediaPlayer, - Recording raw audio with
AudioRecord, - Enhancing audio quality using built-in effects like
Equalizer.
These foundational capabilities empower developers to build rich, interactive audio experiences—particularly valuable in media-centric applications.
In the next chapter, we’ll discuss app signing—a critical step before publishing your application. As we progressively build and refine our app, mastering proper signing procedures will ensure secure distribution and compliance with Google Play requirements.
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after Android Audio Processing: Fundamentals and Best Practices?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue