gauravk95 / audio-visualizer-android

🎵 [Android Library] A light-weight and easy-to-use Audio Visualizer for Android.
Apache License 2.0
844 stars 127 forks source link

mVisualizer.setRawAudioBytes(data) not showing proper data in it.. #4

Open anilp1291 opened 5 years ago

anilp1291 commented 5 years ago

Hey,

I have a recording app and want to use this as visualizer, but when receiving audio and setting into it, i am not able to find the proper blast form or any thing nicer in that..

![image](https://user-images.githubusercontent.com/24537492/51698235-b1cc0380-202f-11e9-921e-6a133b1534c6.png)

    public int RECORDER_SAMPLERATE = 44100;
    private final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

 BufferElements2Rec = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
                RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);

   audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                RECORDER_AUDIO_ENCODING, BufferElements2Rec * 2);

Thread recordingThread = new Thread("recorder") {
                @Override
                public void run() {
                    super.run();
                    if (Looper.myLooper() == null) {
                        Looper.prepare();
                    }
                    audioRecord.setRecordPositionUpdateListener(recordPositionUpdateListener, new Handler(Looper.myLooper()));

                    audioRecord.setPositionNotificationPeriod((int) BufferElements2Rec);
                    //We need to read first chunk to motivate recordPositionUpdateListener.
                    //Mostly, for lower versions - https://code.google.com/p/android/issues/detail?id=53996
                    bufferReadResult = audioRecord.read(buffer, 0, BufferElements2Rec);
                    Looper.loop();
                }
            };
audioRecord.startRecording();

recordingThread.start();

  private AudioRecord.OnRecordPositionUpdateListener recordPositionUpdateListener = new AudioRecord.OnRecordPositionUpdateListener() {
        @Override
        public void onMarkerReached(AudioRecord recorder) {
            //empty for now
        }

        @Override
        public void onPeriodicNotification(AudioRecord recorder) {
            if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING
                    && audioRecord.read(buffer, 0, buffer.length) != -1) {
//                mHorizon.updateView(buffer);
                byte[] data = new byte[buffer.length];
                for (int j = 0; j < buffer.length; j++) {
                    byte tmp = (byte) (buffer[j]);
                    data[j] = tmp;
                }
                  mVisualizer.setRawAudioBytes(data);
            }
        }
    };

Help is really appreciate asap.

Already spent almost two for combo..
![screenshot_10](https://user-images.githubusercontent.com/24537492/51698343-eb9d0a00-202f-11e9-9efa-e5404547f185.png)
GreatWolfXX commented 4 years ago

Привет,

У меня есть приложение для записи, и я хочу использовать его в качестве визуализатора, но при получении звука и настройке в нем я не могу найти правильную форму звука или что-то более приятное в этом ...

![image](https://user-images.githubusercontent.com/24537492/51698235-b1cc0380-202f-11e9-921e-6a133b1534c6.png)

    public int RECORDER_SAMPLERATE = 44100;
    private final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

 BufferElements2Rec = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
                RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);

   audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                RECORDER_AUDIO_ENCODING, BufferElements2Rec * 2);

Thread recordingThread = new Thread("recorder") {
                @Override
                public void run() {
                    super.run();
                    if (Looper.myLooper() == null) {
                        Looper.prepare();
                    }
                    audioRecord.setRecordPositionUpdateListener(recordPositionUpdateListener, new Handler(Looper.myLooper()));

                    audioRecord.setPositionNotificationPeriod((int) BufferElements2Rec);
                    //We need to read first chunk to motivate recordPositionUpdateListener.
                    //Mostly, for lower versions - https://code.google.com/p/android/issues/detail?id=53996
                    bufferReadResult = audioRecord.read(buffer, 0, BufferElements2Rec);
                    Looper.loop();
                }
            };
audioRecord.startRecording();

recordingThread.start();

  private AudioRecord.OnRecordPositionUpdateListener recordPositionUpdateListener = new AudioRecord.OnRecordPositionUpdateListener() {
        @Override
        public void onMarkerReached(AudioRecord recorder) {
            //empty for now
        }

        @Override
        public void onPeriodicNotification(AudioRecord recorder) {
            if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING
                    && audioRecord.read(buffer, 0, buffer.length) != -1) {
//                mHorizon.updateView(buffer);
                byte[] data = new byte[buffer.length];
                for (int j = 0; j < buffer.length; j++) {
                    byte tmp = (byte) (buffer[j]);
                    data[j] = tmp;
                }
                  mVisualizer.setRawAudioBytes(data);
            }
        }
    };

Help is really appreciate asap.

Already spent almost two for combo..
![screenshot_10](https://user-images.githubusercontent.com/24537492/51698343-eb9d0a00-202f-11e9-9efa-e5404547f185.png)

Hi, you have solved this problem. If so, how.