在 Android 开发中,使用 MediaPlayer
播放音频时,可以通过 EnvironmentalReverb
和 PresetReverb
来添加混响效果。然而,混响效果可能不会直接应用于 MediaPlayer
,因为 MediaPlayer
的音频输出路径可能不支持这些效果。为了确保混响效果能够应用于音频播放,您可以尝试以下步骤:
AudioTrack
代替 MediaPlayer
AudioTrack
提供了更低级别的音频播放控制,允许您更灵活地应用音频效果。以下是一个示例,展示如何使用 AudioTrack
和 EnvironmentalReverb
来播放音频并应用混响效果。
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.audiofx.EnvironmentalReverb;
import android.media.audiofx.PresetReverb;
import android.os.Build;
import android.util.Log;
import java.io.IOException;
import java.nio.ByteBuffer;
public class AudioPlayer {
private static final String TAG = "AudioPlayer";
private static final int BUFFER_SIZE = 1024 * 1024;
private AudioTrack audioTrack;
private EnvironmentalReverb environmentalReverb;
private PresetReverb presetReverb;
public void playAudio(String filePath) {
MediaExtractor extractor = new MediaExtractor();
try {
extractor.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
return;
}
MediaFormat format = null;
for (int i = 0; i < extractor.getTrackCount(); i++) {
format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (mime.startsWith("audio/")) {
extractor.selectTrack(i);
break;
}
}
if (format == null) {
Log.e(TAG, "No audio track found in file");
return;
}
int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
int channelConfig = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT) == 1 ?
AudioFormat.CHANNEL_OUT_MONO : AudioFormat.CHANNEL_OUT_STEREO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat);
audioTrack = new AudioTrack(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build(),
new AudioFormat.Builder()
.setSampleRate(sampleRate)
.setChannelMask(channelConfig)
.setEncoding(audioFormat)
.build(),
Math.max(minBufferSize, BUFFER_SIZE),
AudioTrack.MODE_STREAM,
AudioManager.AUDIO_SESSION_ID_GENERATE
);
// Initialize EnvironmentalReverb
environmentalReverb = new EnvironmentalReverb(0, audioTrack.getAudioSessionId());
environmentalReverb.setDecayTime(5000);
environmentalReverb.setDensity((short) 1000);
environmentalReverb.setEnabled(true);
// Initialize PresetReverb
presetReverb = new PresetReverb(0, audioTrack.getAudioSessionId());
presetReverb.setPreset(PresetReverb.PRESET_LARGEHALL);
presetReverb.setEnabled(true);
audioTrack.play();
ByteBuffer inputBuffer = ByteBuffer.allocate(BUFFER_SIZE);
while (true) {
int sampleSize = extractor.readSampleData(inputBuffer, 0);
if (sampleSize < 0) {
break;
}
byte[] buffer = new byte[sampleSize];
inputBuffer.get(buffer);
inputBuffer.clear();
audioTrack.write(buffer, 0, sampleSize);
extractor.advance();
}
audioTrack.stop();
audioTrack.release();
environmentalReverb.release();
presetReverb.release();
}
}
MediaPlayer
和 AuxEffect
如果您仍然希望使用 MediaPlayer
,可以尝试将混响效果作为辅助效果(AuxEffect)应用到 MediaPlayer
。以下是一个示例,展示如何将 EnvironmentalReverb
和 PresetReverb
应用于 MediaPlayer
。
import android.media.MediaPlayer;
import android.media.audiofx.EnvironmentalReverb;
import android.media.audiofx.PresetReverb;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private EnvironmentalReverb environmentalReverb;
private PresetReverb presetReverb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.your_audio_file); // Replace with your audio file
// Initialize EnvironmentalReverb
environmentalReverb = new EnvironmentalReverb(0, mediaPlayer.getAudioSessionId());
environmentalReverb.setDecayTime(5000);
environmentalReverb.setDensity((short) 1000);
environmentalReverb.setEnabled(true);
// Initialize PresetReverb
presetReverb = new PresetReverb(0, mediaPlayer.getAudioSessionId());
presetReverb.setPreset(PresetReverb.PRESET_LARGEHALL);
presetReverb.setEnabled(true);
// Attach the effects to the MediaPlayer
mediaPlayer.attachAuxEffect(environmentalReverb.getId());
mediaPlayer.setAuxEffectSendLevel(1.0f);
mediaPlayer.attachAuxEffect(presetReverb.getId());
mediaPlayer.setAuxEffectSendLevel(1.0f);
mediaPlayer.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
}
if (environmentalReverb != null) {
environmentalReverb.release();
}
if (presetReverb != null) {
presetReverb.release();
}
}
}
领取专属 10元无门槛券
手把手带您无忧上云