I want to get the intensity of music(to decide the size and position of the tile which is generated according to the music) in the native build specially for android

I have tried these things and many other way but not able to achieve the goal.

MyAudioPlugin.java

package com.cocos.game;

import android.media.MediaPlayer;

import android.media.audiofx.Visualizer;

import android.webkit.JavascriptInterface;

public class MyAudioPlugin {

private static MyAudioPlugin instance;

private MediaPlayer mediaPlayer;

private Visualizer visualizer;

private OnIntensityListener intensityListener;

// Private constructor to enforce singleton pattern

private MyAudioPlugin() {

    mediaPlayer = new MediaPlayer();

}

// Singleton getInstance method

public static MyAudioPlugin getInstance() {

    if (instance == null) {

        instance = new MyAudioPlugin();

    }

    return instance;

}

// Load audio file method called from JavaScript

@JavascriptInterface

public void loadAudioFile(String filePath) {

    try {

        // Set the data source to the audio file path

        mediaPlayer.setDataSource(filePath);

        // Prepare the MediaPlayer asynchronously

        mediaPlayer.prepareAsync();

    } catch (Exception e) {

        e.printStackTrace();

    }

}

// Start playing the loaded audio file

@JavascriptInterface

public void play() {

    mediaPlayer.start();

    // Initialize Visualizer

    int audioSessionId = mediaPlayer.getAudioSessionId();

    visualizer = new Visualizer(audioSessionId);

    visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

    visualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {

        @Override

        public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate) {

            // Not used in this example

        }

        @Override

        public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {

            // Calculate intensity based on FFT data

            float intensity = calculateIntensityFromFft(fft);

            // Pass intensity to listener

            if (intensityListener != null) {

                intensityListener.onIntensity(intensity);

            }

        }

    }, Visualizer.getMaxCaptureRate() / 2, true, false);

    visualizer.setEnabled(true);

}

// Pause the audio playback

@JavascriptInterface

public void pause() {

    mediaPlayer.pause();

}

// Stop the audio playback and release resources

@JavascriptInterface

public void stop() {

    mediaPlayer.stop();

    mediaPlayer.release();

    mediaPlayer = null;

    if (visualizer != null) {

        visualizer.release();

        visualizer = null;

    }

}

// Set the listener for intensity updates

public void setOnIntensityListener(OnIntensityListener listener) {

    intensityListener = listener;

}

// Calculate intensity based on FFT data

private float calculateIntensityFromFft(byte[] fft) {

    // Calculate intensity based on FFT data

    // You need to implement your own logic here

    // This is just a placeholder implementation

    float sum = 0;

    for (byte b : fft) {

        sum += Math.abs(b);

    }

    return sum / fft.length;

}

// Listener interface for intensity updates

public interface OnIntensityListener {

    void onIntensity(float intensity);

}

}

AudioManager.ts

import { game, native, sys } from ‘cc’;

// AudioManager.ts

let AudioManager = {

_plugin: null,

init(filePath: string) {

    console.log("audio manager");

    if (sys.os == sys.OS.ANDROID && sys.isNative) {

        native.reflection.callStaticMethod("com/cocos/game/MyAudioPlugin", "loadAudioFile", "(Ljava/lang/String;)V", filePath);

        // this._plugin = native.reflection.callStaticMethod("com/cocos/game/MyAudioPlugin", "getInstance", "()Lcom/cocos/game/object;");

        // let c = native.reflection.callStaticMethod('com/cocos/game/MyAudioPlugin', "sum", "(II)I", 3, 7);

        //console.log("result :" + c);

        // call hello

        //native.reflection.callStaticMethod("com/cocos/game/Test", "hello", "(Ljava/lang/String;)V", "this is a message from JavaScript");

        // call the first sum

        var result = native.reflection.callStaticMethod("com/cocos/game/Test", "sum", "(II)I", 3, 7);

        console.log(result); // 10

        // call the second sum

        var result = native.reflection.callStaticMethod("com/cocos/game/Test", "sum", "(I)I", 3);

        console.log(result); // 5

    }

    console.log("after initialization");

},

loadAudioFile(filePath: string) {

    native.reflection.callStaticMethod('com/cocos/game/MyAudioPlugin', 'loadAudioFile', '()V', filePath);

},

play() {

    native.reflection.callStaticMethod('com/cocos/game/MyAudioPlugin', 'play', '()V');

},

pause() {

    native.reflection.callStaticMethod('com/cocos/game/MyAudioPlugin', 'pause', '()V');

},

onIntensityReceived(intensity: number) {

    // Use the intensity value as needed

    console.log("Intensity received from native code:", intensity);

},

};

export default AudioManager;

These two pages could help you solve this problem.
https://docs.cocos.com/creator/manual/zh/advanced-topics/java-reflection.html
https://docs.cocos.com/creator/manual/zh/advanced-topics/js-java-bridge.html