title: Drawing a waveform from a short AAC file on Android
body: <p>I'm trying to draw a waveform for a short AAC audio file (can't convert to WAV as they come from a web service) on Android, and it's proving surprisingly tricky.</p>
  
  <p>Here is an image of what I'm trying to achieve: </p>
  
  <p><img src="https://i.stack.imgur.com/JhGWm.png" alt="enter image description here"></p>
  
  <p>Note that the frame of the view will be fixed proportionally to the device's screen resolution, and will not need to stretch, or be zoomed at all. This means I can just set up an array of height values with length equal to the pixel width of the view at runtime. Also these audio files are short, so I'm not really worried about memory right now.</p>
  
  <p>Now, if I understand corrently, the necessary steps are basically:</p>
  
  <ol>
  <li>Decode the AAC audio into an array of bytes representing the amplitude of the wave.</li>
  <li>Pull out a byte for each pixel on the x-axis to be displayed.</li>
  <li>Override onDraw in my view and draw a line for each pixel on the x-axis.</li>
  </ol>
  
  <p>However, I'm having trouble with step 1. I've tried:</p>
  
  <ol>
  <li><p>JAAD (<a href="http://jaadec.sourceforge.net/usage.php" rel="nofollow noreferrer">http://jaadec.sourceforge.net/usage.php</a>), which looked promising, only to discover that it won't run on Android due to the absence of javax.sound.sampled.</p></li>
  <li><p>The code provided in the answer in <a href="https://stackoverflow.com/a/11858709/269152">https://stackoverflow.com/a/11858709/269152</a> which actually works rather nicely to handle all steps (with a little editing of the code to remove all the zooming etc), except the waveform is really blocky, I think due to the way the audio is cut up into a small number of "frames", perhaps due to this being intended for longer files and zoomed by the user. I've been able to get more detail in the WAV handling by editing the CheapWAV class, but am not sure how to do it with the CheapAAC class.</p></li>
  <li><p>Android's MediaCodec is not an option due to needing Android 4.1+. I need to support 2.3.</p></li>
  <li><p>I've tried Android's Visualizer class in conjunction with a MediaPlayer, but all I was getting out of it was a 1024 length array of -128 values. It looks like the visualizer isn't designed to be used to scan files, but just to visualize playback as it happens? I can post the code if it's of any use. (<a href="http://developer.android.com/reference/android/media/audiofx/Visualizer.html" rel="nofollow noreferrer">http://developer.android.com/reference/android/media/audiofx/Visualizer.html</a>) </p></li>
  </ol>
  
  <p>This was really easy on iOS using AVAssetReader to decode the audio for me. Am I missing something obvious? Is there an easy way for me to get the PCM values for an AAC file so I can plot them in my view? Or does someone know of an awesome view that handles all of this?</p>
  
  <p>Many thanks in advance for any help!</p>