我想多次使用audio-visualizer-android 的可视化工具。
据我所知,图书馆不支持,但我在这里收到了很好的答案。
这是它的代码:
public class CopyBarVisualizer extends BarVisualizer {
private List<UpdateListener> listeners = new ArrayList<>();
public CopyBarVisualizer(Context context) {
super(context);
}
public CopyBarVisualizer(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CopyBarVisualizer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void addUpdateListener(UpdateListener listener) {
listeners.add(listener);
}
public void copyFrom(CopyBarVisualizer other) {
other.addUpdateListener(new UpdateListener() {
@Override
public void update(byte[] data) {
mRawAudioBytes = data;
CopyBarVisualizer.this.invalidate();
}
});
}
@Override
public void invalidate() {
super.invalidate();
for (UpdateListener listener : listeners) {
listener.update(mRawAudioBytes);
}
}
public interface UpdateListener {
void update(byte[] data);
}
}
Run Code Online (Sandbox Code Playgroud)
初始化方法:
copyVisualizer.setAudioSessionId(audioSessionId);
copyVisualizer2.copyFrom(copyVisualizer);
Run Code Online (Sandbox Code Playgroud)
添加一名作者
使用此代码,我可以复制 BarVisualizer 一次,但不能多次复制。
这就是我多次尝试复制它的方式:
copyVisualizer.setAudioSessionId(audioSessionId);
copyVisualizer2.copyFrom(copyVisualizer); // Works fine
copyVisualizer3.copyFrom(copyVisualizer); // Doesn't work
Run Code Online (Sandbox Code Playgroud)
但后来我得到了错误:
尝试在空对象引用上调用虚拟方法“void com.example.app.classes.CopyBarVisualizer.copyFrom(com.example.app.classes.CopyBarVisualizer)”
这是我的 XML 的样子:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
<com.example.app.classes.CopyBarVisualizer xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/copyVisualizer"
android:layout_width="100dp"
android:layout_height="150dp"
custom:avColor="#D11817"
custom:avDensity="0.0"
custom:avGravity="bottom"
custom:avSpeed="fast"
custom:avWidth="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
<com.example.app.classes.CopyBarVisualizer xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/copyVisualizer2"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_marginTop="150dp"
android:layout_marginRight="2dp"
android:layout_marginEnd="2dp"
android:rotation="180"
app:avColor="#D11817"
app:avDensity="2.5"
app:avGravity="bottom"
app:avSpeed="fast"
app:avWidth="40dp"
/>
<com.example.app.classes.CopyBarVisualizer xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/copyVisualizer3"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_marginTop="150dp"
android:layout_marginRight="100dp"
android:layout_marginEnd="2dp"
android:rotation="180"
app:avColor="#D11817"
app:avDensity="2.5"
app:avGravity="bottom"
app:avSpeed="fast"
app:avWidth="40dp"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能多次显示可视化工具呢?
| 归档时间: |
|
| 查看次数: |
98 次 |
| 最近记录: |