【聊天 app 源码】【bomb使用源码】【lx 商城源码】android 波形 源码_android波形图
1.android如何做到播放音乐时动态波形
android如何做到播放音乐时动态波形
1. 确保MediaPlayer正确创建并播放音乐。波波形
2. 检查Visualizer和Equalizer的形源聊天 app 源码实例化是否在正确的生命周期内。
3. 确保在Activity暂停时正确释放Visualizer、波波形bomb使用源码Equalizer和MediaPlayer资源。形源lx 商城源码
4. 修改VisualizerView的波波形delphi 10 源码onDraw方法,确保正确绘制波形和频谱。形源新闻 文章 源码
5. 添加必要的波波形错误处理和日志输出,以便调试。形源
修改后的波波形代码如下:
```java
package com.AudioFx;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Equalizer;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class AudioFxActivity extends Activity {
private static final String TAG = "AudioFxActivity";
private static final float VISUALIZER_HEIGHT_DIP = f;
private MediaPlayer mMediaPlayer;
private Visualizer mVisualizer;
private Equalizer mEqualizer;
private LinearLayout mLinearLayout;
private VisualizerView mVisualizerView;
private TextView mStatusTextView;
private TextView mInfoView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mStatusTextView = new TextView(this);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mLinearLayout.addView(mStatusTextView);
setContentView(mLinearLayout);
// Create the MediaPlayer
try {
mMediaPlayer = MediaPlayer.create(this, R.raw.my_life);
} catch (Exception e) {
Log.e(TAG, "Error creating MediaPlayer", e);
finish();
return;
}
Log.d(TAG, "MediaPlayer audio session ID: " + mMediaPlayer.getAudioSessionId());
setupVisualizerFxAndUI();
setupEqualizerFxAndUI();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mStatusTextView.setText("Playing music...");
}
private void setupEqualizerFxAndUI() {
// Create the Equalizer object (an AudioEffect subclass) and attach it
// to our media player, with a default priority (0).
mEqualizer = new Equalizer(0, mMediaPlayer.getAudioSessionId());
mEqualizer.setEnabled(true);
TextView eqTextView = new TextView(this);
eqTextView.setText("Equalizer:");
mLinearLayout.addView(eqTextView);
short bands = mEqualizer.getNumberOfBands();
final short minEQLevel = mEqualizer.getBandLevelRange()[0];
final short maxEQLevel = mEqualizer.getBandLevelRange()[1];
for (short i = 0; i < bands; i++) {
final short band = i;
TextView freqTextView = new TextView(this);
freqTextView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
freqTextView.setGravity(Gravity.CENTER_HORIZONTAL);
freqTextView.setText((mEqualizer.getCenterFreq(band) / ) + " Hz");
mLinearLayout.addView(freqTextView);
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
TextView minDbTextView = new TextView(this);
minDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
minDbTextView.setText((minEQLevel / ) + " dB");
row.addView(minDbTextView);
TextView maxDbTextView = new TextView(this);
maxDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
maxDbTextView.setText((maxEQLevel / ) + " dB");
row.addView(maxDbTextView);
SeekBar bar = new SeekBar(this);
bar.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(mEqualizer.getBandLevel(band));
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mEqualizer.setBandLevel(band, (short) (progress + minEQLevel));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
row.addView(bar);
mLinearLayout.addView(row);
}
}
private void setupVisualizerFxAndUI() {
mVisualizerView = new VisualizerView(this);
mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
(int) (VISUALIZER_HEIGHT_DIP * getResources().getDisplayMetrics().density)));
mLinearLayout.addView(mVisualizerView);
mInfoView = new TextView(this);
String infoStr = "";
int[] csr = Visualizer.getCaptureSizeRange();
if (csr != null) {
String csrStr = "CaptureSizeRange: ";
for (int i = 0; i < csr.length; i++) {
csrStr += csr[i];
csrStr += " ";
}
infoStr += csrStr;
}
final int maxCR = Visualizer.getMaxCaptureRate();
infoStr = infoStr + "\nMaxCaptureRate: " + maxCR;
mInfoView.setText(infoStr);
mLinearLayout.addView(mInfoView);
mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());
mVisualizer.setCaptureSize();
mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
mVisualizerView.updateVisualizer(bytes);
}
@Override
public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {
mVisualizerView.updateVisualizer(fft);
}
}, maxCR / 2, false, true);
}
@Override
protected void onPause() {
super.onPause();