Tuesday, 25 May 2021

Visualizer gets copied but not more than once

I would like to use the visualizer of audio-visualizer-android multiple times.

As far as I know, the library doesn't support that but I have received a very good answer on here.

This is the code of it:

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);
    }
}

The way to initialize it:

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

Author: F43nd1r

With this code, I am able to copy BarVisualizer once but not more.

This is how I tried to copy it more than once:

copyVisualizer.setAudioSessionId(audioSessionId);
copyVisualizer2.copyFrom(copyVisualizer); // Works fine
copyVisualizer3.copyFrom(copyVisualizer); // Doesn't work

but then I get the error:

Attempt to invoke virtual method 'void com.example.app.classes.CopyBarVisualizer.copyFrom(com.example.app.classes.CopyBarVisualizer)' on a null object reference

This is how my XML looks like:

<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>

So how can I display the visualizer more than twice?



from Visualizer gets copied but not more than once

No comments:

Post a Comment