Skip to content Skip to sidebar Skip to footer

How To Display 2 Barvisualizers?

I am using the library audio-visualizer-android. I am getting BarVisualizer from the constructor of my class and use the following code to display the BarVisualizer: int audioSessi

Solution 1:

This is an issue that should be raised with the library author(s), as it doesn't support multiple visualizers attached to the same audio session.


But you can hack in that functionality by extending BarVisualizer and using that new class everywhere as a replacement:

publicclassCopyBarVisualizerextendsBarVisualizer {
    private List<UpdateListener> listeners = newArrayList<>();

    publicCopyBarVisualizer(Context context) {
        super(context);
    }

    publicCopyBarVisualizer(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    publicCopyBarVisualizer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    publicvoidaddUpdateListener(UpdateListener listener) {
        listeners.add(listener);
    }

    publicvoidcopyFrom(CopyBarVisualizer other) {
        other.addUpdateListener(newUpdateListener() {
            @Overridepublicvoidupdate(byte[] data) {
                mRawAudioBytes = data;
                CopyBarVisualizer.this.invalidate();
            }
        });
    }

    @Overridepublicvoidinvalidate() {
        super.invalidate();
        for (UpdateListener listener : listeners) {
            listener.update(mRawAudioBytes);
        }
    }

    publicinterfaceUpdateListener {
        voidupdate(byte[] data);
    }
}

Then initialize like this:

copyVisualizer.setAudioSessionId(audioSessionId);
copyVisualizer2.copyFrom(copyVisualizer);

Post a Comment for "How To Display 2 Barvisualizers?"