Android Open Source - Resonos-Android-Framework Audio Visualizer






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.resonos.apps.library.media;
/*from   w ww . j a v  a  2  s  . c om*/
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;

import com.resonos.apps.library.App;

/**
 * This class makes using the Android visualizer object a bit easier
 * based on some code from https://github.com/felixpalmer/android-visualizer/
 * Thread safe.
 * @author Chris
 *
 */
public class AudioVisualizer {
  
  // vars
  private byte[] mBytes;
  private byte[] mFFTBytes;
  
  // objects
  private Visualizer mVisualizer;

  /**
   * Create a visualizer object
   * @param app object
   */
  public AudioVisualizer(App app) {
    init();
  }

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

  /**
   * Links the visualizer to a MediaPlayer
   * @param player - MediaPlayer instance to link to
   */
  public synchronized void link(MediaPlayer player) {
    if (mVisualizer != null)
      mVisualizer.release();
    
    // Create the Visualizer object and attach it to our media player.
    if (player == null)
      mVisualizer = new Visualizer(0);
    else
      mVisualizer = new Visualizer(player.getAudioSessionId());

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

    // Pass through Visualizer data to VisualizerView
    Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener() {
      @Override
      public void onWaveFormDataCapture(Visualizer visualizer,
          byte[] bytes, int samplingRate) {
        updateVisualizer(bytes);
      }

      @Override
      public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
          int samplingRate) {
        updateVisualizerFFT(bytes);
      }
    };

    mVisualizer.setDataCaptureListener(captureListener,
        Visualizer.getMaxCaptureRate() / 2, true, true);

    // Enabled Visualizer and disable when we're done with the stream
    mVisualizer.setEnabled(true);
    if (player != null)
      player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
          mVisualizer.setEnabled(false);
        }
      });
  }

  /**
   * Free resources
   */
  public synchronized void release() {
    if (mVisualizer != null)
      mVisualizer.release();
  }

  private synchronized void updateVisualizer(byte[] bytes) {
    mBytes = bytes;
  }
  
  /**
   * @return A byte array of the most recent audio data.
   */
  public synchronized byte[] getAudioData() {
    return mBytes;
  }
  
  private synchronized void updateVisualizerFFT(byte[] bytes) {
    mFFTBytes = bytes;
  }
  
  /**
   * @return A byte array of the most recent audio FFT data.
   */
  public synchronized byte[] getAudioFFTData() {
    return mFFTBytes;
  }
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java