gpt4 book ai didi

java - 如何在 Android 应用程序中录制/播放音频时创建 sinwave 动画?

转载 作者:太空狗 更新时间:2023-10-29 15:31:39 24 4
gpt4 key购买 nike

在 iOS 应用程序中很容易创建。谁能告诉我在录制和播放音频时创建 Sinwave 动画的简单方法。我尝试了很多方法,但我做不到。是否有任何第三方框架?

谁能帮帮我..

最佳答案

使用下面的代码像 ios 一样获取波形...从录音机获取振幅并传递给 updateAmplitude() 函数以获取语音的变化。

public class WaveFormView extends View {

private static final float defaultFrequency = 1.5f;
private static final float defaultAmplitude = 1.0f;
private static final float defaultIdleAmplitude = 0.01f;
private static final float defaultNumberOfWaves = 5.0f;
private static final float defaultPhaseShift = -0.15f;
private static final float defaultDensity = 5.0f;
private static final float defaultPrimaryLineWidth = 3.0f;
private static final float defaultSecondaryLineWidth = 1.0f;

private float phase;
private float amplitude;
private float frequency;
private float idleAmplitude;
private float numberOfWaves;
private float phaseShift;
private float density;
private float primaryWaveLineWidth;
private float secondaryWaveLineWidth;
Paint mPaintColor;
Rect rect;
boolean isStraightLine = false;

public WaveFormView(Context context) {
super(context);
setUp();
}

public WaveFormView(Context context, AttributeSet attrs) {
super(context, attrs);
setUp();
}

public WaveFormView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setUp();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public WaveFormView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setUp();
}

private void setUp() {
this.frequency = defaultFrequency;

this.amplitude = defaultAmplitude;
this.idleAmplitude = defaultIdleAmplitude;

this.numberOfWaves = defaultNumberOfWaves;
this.phaseShift = defaultPhaseShift;
this.density = defaultDensity;

this.primaryWaveLineWidth = defaultPrimaryLineWidth;
this.secondaryWaveLineWidth = defaultSecondaryLineWidth;
mPaintColor = new Paint();
mPaintColor.setColor(Color.WHITE);
}

public void updateAmplitude(float ampli, boolean isSpeaking) {
this.amplitude = Math.max(ampli, idleAmplitude);
isStraightLine = isSpeaking;
}


@Override
protected void onDraw(Canvas canvas) {
rect = new Rect(0,0,canvas.getWidth(),canvas.getWidth());
canvas.drawColor(Color.BLUE);
/*canvas.drawRect(rect, mPaintColor);*/
if(isStraightLine) {
for (int i = 0; i < numberOfWaves; i++) {
mPaintColor.setStrokeWidth(i == 0 ? primaryWaveLineWidth : secondaryWaveLineWidth);
float halfHeight = canvas.getHeight() / 2;
float width = canvas.getWidth();
float mid = canvas.getWidth() / 2;

float maxAmplitude = halfHeight - 4.0f;
float progress = 1.0f - (float) i / this.numberOfWaves;
float normedAmplitude = (1.5f * progress - 0.5f) * this.amplitude;
Path path = new Path();

float multiplier = Math.min(1.0f, (progress / 3.0f * 2.0f) + (1.0f / 3.0f));

for (float x = 0; x < width + density; x += density) {
// We use a parable to scale the sinus wave, that has its peak in the middle of the view.
float scaling = (float) (-Math.pow(1 / mid * (x - mid), 2) + 1);

float y = (float) (scaling * maxAmplitude * normedAmplitude * Math.sin(2 * Math.PI * (x / width) * frequency + phase) + halfHeight);

if (x == 0) {
path.moveTo(x, y);
} else {
path.lineTo(x, y);
}
}
mPaintColor.setStyle(Paint.Style.STROKE);
mPaintColor.setAntiAlias(true);
canvas.drawPath(path, mPaintColor);

}
} else {
canvas.drawLine(5,canvas.getHeight()/2,canvas.getWidth(),canvas.getHeight()/2,mPaintColor );
canvas.drawLine(0,canvas.getHeight()/2,canvas.getWidth(),canvas.getHeight()/2,mPaintColor);
canvas.drawLine(-5, canvas.getHeight() / 2, canvas.getWidth(), canvas.getHeight()/2,mPaintColor );
}
this.phase += phaseShift;
invalidate();
}
}

关于java - 如何在 Android 应用程序中录制/播放音频时创建 sinwave 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33162944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com