Hi,

When I call getWaveForm method of Visualizer class, most of time I get all 
-128 in byte array and sometimes I get different values in byte array (this 
is correct). I though it may be sound volume issue, but applied both high 
volume and low volume in code and got same result. My sample audio file 
length is 2/3 seconds.

I have attached my file, plz check it. Dont be confused to  see the code, 
basically  it is a PhoneGap plugin part. Please see the Android part 
(especially getWaveForm() of Visualizer class), that I am facing issue.

My question is why I am getting some times correct value in byte array. 
Though most of time I am getting -128 in array.

This can not be a solution, I need a stable solution. I need getWaveForm 
method will return different values (not all -128).

Any idea?


-arefin

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
package com.first;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Equalizer;
import android.media.audiofx.Visualizer;
import android.util.Log;
import org.apache.cordova.*;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;

public class Audio2Wave extends Plugin {

	private static final String CONVERT = "convert";
	private static final int JSON_INDEX = 0;
	// private String output="";

	MediaPlayer mp;

	// Vizualization
	private Visualizer mVisualizer;

	@Override
	public PluginResult execute(String arg0, final JSONArray arg1, String arg2) {
		// TODO Auto-generated method stub
		if (arg0.equals(CONVERT)) {

			try {
				String url = arg1.getString(JSON_INDEX);

				// start media player - like normal
				
				
				mp = new MediaPlayer();
				mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

				try {
					mp.setDataSource(url); // set data source our URL
											// defined
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				try { // tell your player to go to prepare state
					mp.prepare();
					// mp.prepareAsync();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				// Start stream / player
				mp.start();
				// mp.setVolume(1,1);

				// Visualizer object and attach it to media player.
				// NEED android.permission.RECORD_AUDIO for that in
				// AndroidManifest.xml
				mVisualizer = new Visualizer(mp.getAudioSessionId());
				mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

				mVisualizer.setDataCaptureListener(
						new Visualizer.OnDataCaptureListener() {
							public void onWaveFormDataCapture(
									Visualizer visualizer, byte[] bytes,
									int samplingRate) {

							}

							public void onFftDataCapture(Visualizer visualizer,
									byte[] bytes, int samplingRate) {
							}
						}, Visualizer.getMaxCaptureRate() / 2, true, false);

				// enable vizualizer
				mVisualizer.setEnabled(true);

				try {

					String output = "";
					int captureSize = mVisualizer.getCaptureSize();
					byte[] waves = new byte[captureSize];

					// conver to waveform
					int result = mVisualizer.getWaveForm(waves);

					if (result == 0) {
						// success
						Log.i("arefin", "success");

						// conver byte array to sting
						for (int i = 0; i < waves.length; i++) {
							output = output + waves[i] + ",";
						}
						if (output.length() != 0) {
							output = output.substring(0, output.length() - 1);
						}

					} else if (result == -6) {
						// no memory
						Log.i("arefin", "no memory");
					} else if (result == -5) {
						// invalid operation
						Log.i("arefin", "invalid operation");
					} else if (result == -7) {
						// Operation failed due to dead remote object.
						Log.i("arefin",
								"Operation failed due to dead remote object");
					}

					// disable the visualizer

					if (!mp.isPlaying()) {

						mVisualizer.setEnabled(false);
						mVisualizer.release();
						mp.release();
						mp = null;
					}

					return new PluginResult(PluginResult.Status.OK, output);

				} catch (IllegalStateException er) {
					er.printStackTrace();
				} catch (Exception ex) {
					ex.printStackTrace();
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
			}
		} else {
			return new PluginResult(PluginResult.Status.INVALID_ACTION);
		}

		return new PluginResult(PluginResult.Status.INVALID_ACTION);

	}
}

Reply via email to