I can't see visualizer while recording voice on Android

3.4k views Asked by At

Update : I unfortunately can't get a solution for my question yet...

I am developing a recorder on android and I want show visualize while recording. I have bellow classes. When I run project and press start record I cant see any visualizer while recording.I test bellow code on real device and add permissions to manifest.

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

I use from open source project visualizer for playing by "Felix Palmer" and own add new method for link recorder to visualizer. I think must be run perfectly.but really any showing for my recorder unfortunately. (code has not any error) How i can resolve my problem and see visualizer? This is VisualizerView.java class...

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.audiofx.Visualizer;
import android.util.AttributeSet;
import android.view.View;
import com.pheelicks.visualizer.renderer.Renderer;

public class VisualizerView extends View {

  private byte[] mBytes;
  private byte[] mFFTBytes;
  private Rect mRect = new Rect();
  private Visualizer mVisualizer;

  private Set<Renderer> mRenderers;

  private Paint mFlashPaint = new Paint();
  private Paint mFadePaint = new Paint();

  public VisualizerView(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs);
    init();
  }

  public VisualizerView(Context context, AttributeSet attrs)
  {
    this(context, attrs, 0);
  }

  public VisualizerView(Context context)
  {
    this(context, null, 0);
  }

  private void init() {
    mBytes = null;
    mFFTBytes = null;

    mFlashPaint.setColor(Color.argb(122, 255, 255, 255));
    mFadePaint.setColor(Color.argb(238, 255, 255, 255)); // Adjust alpha to change how quickly the image fades
    mFadePaint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));

    mRenderers = new HashSet<Renderer>();
  }
//-----------------------------------------------
  public void link(MediaRecorder recorder)
  {
    if(recorder == null)
    {
      throw new NullPointerException("Cannot link to null MediaPlayer");
    }
    mVisualizer = new Visualizer(0);
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

    Visualizer.OnDataCaptureListener datacaptureListener1=new  Visualizer.OnDataCaptureListener() 
     {

        @Override
        public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                int samplingRate) 
        {
            // TODO Auto-generated method stub
              updateVisualizer(bytes);
        }

        @Override
        public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
                int samplingRate) 
        {
            // TODO Auto-generated method stub
            updateVisualizerFFT(bytes);
        }
    };
      mVisualizer.setDataCaptureListener(datacaptureListener1,Visualizer.getMaxCaptureRate() /2,false,true);
        mVisualizer.setEnabled(true);
  }

  //-----------------------------------------------

This is AudioRecorder.java...

public class AudioRecorder 
{
    private String name="";
    private static int id=0;
    private MediaRecorder recorder = new MediaRecorder();
    private String path=null;
   VisualizerView mVisualizerView;
   private Context context=null;
    //-----------------------------------------------   
     public AudioRecorder(Context context)
     {
         this.context=context;
     }

    //-----------------------------------------------        

     public void Record() throws IOException 
        {

            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(this.path); 
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.recorder1, null);
            mVisualizerView = (VisualizerView)view.findViewById(R.id.visualizer1);
               mVisualizerView.link(recorder);
               addLineRenderer();
            try 
            {
               recorder.prepare();
            } 
            catch (IllegalStateException e) 
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            try 
            {
                System.out.println("****");
                recorder.start();
            } 
            catch (Exception e) 
            {
            }

        }

        //------------------------------------------     

        public void stopRecord() throws IOException 
        {           
            recorder.stop();
            recorder.release();

        }

And this is visualizer in my XML.

<LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_weight="1"
               android:background="@drawable/highlight_shared"
               android:orientation="vertical"
               android:paddingLeft="10dp"
               android:paddingRight="10dp"
               android:paddingTop="5dp" >

               <FrameLayout
                   android:layout_width="fill_parent"
                   android:layout_height="0dp"
                   android:layout_weight="1"
                   android:background="@drawable/corner_liner" >
                 <com.pheelicks.visualizer.VisualizerView
                    android:id="@+id/visualizer1"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"/>
               </FrameLayout>
           </LinearLayout>
1

There are 1 answers

0
clark.li On

It is the issue with certain phones for different reasons. You can see here https://code.google.com/p/android/issues/detail?id=64423 https://github.com/felixpalmer/android-visualizer/issues/5 for the list of unsupported devices.