How to run audio file using Audiotrack class?

Asked

Viewed 771 times

2

I intend to make an app to play drums, but, as I’m starting now to develop for android, I’m having difficulty. I need to play an audio file with the class AudioTrack (because it has the lowest latency), but I didn’t understand how to use it to run a file .mp3, for example.

I’ve researched, but I’ve only found examples of the class MediaPlayer. Could someone post an example of AudioTrack?

1 answer

2

The only way to play some audio by AudioTrack is a streaming PCM, as can be seen in documentation:

The AudioTrack class manages and plays a single audio Resource for Java Applications. It Allows streaming of PCM audio buffers to the audio Sink for playback. This is achieved by "pushing" the data to the Audiotrack Object using one of the write(byte[], int, int) and write(short[], int, int) methods.

In free translation:

The class AudioTrack manage and play an audio resource for Java applications. It allows streaming of buffers PCM audio be the audio source for playback. This can be achieved by loading the information to an object AudioTrack using one of the methods: write(byte[], int, int) and wirte(short[], int, int).

Can be used this tutorial to decompress the file .mp3 and load it as PCM, using the jlayer library and the code below:

public static byte[] decode(String path, int startMs, int maxMs)
  throws IOException, com.mindtherobot.libs.mpg.DecoderException {
  ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);

  float totalMs = 0;
  boolean seeking = true;

  File file = new File(path);
  InputStream inputStream = new BufferedInputStream(new FileInputStream(file), 8 * 1024);
  try {
    Bitstream bitstream = new Bitstream(inputStream);
    Decoder decoder = new Decoder();

    boolean done = false;
    while (! done) {
      Header frameHeader = bitstream.readFrame();
      if (frameHeader == null) {
        done = true;
      } else {
        totalMs += frameHeader.ms_per_frame();

        if (totalMs >= startMs) {
          seeking = false;
        }

        if (! seeking) {
          SampleBuffer output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitstream);

          if (output.getSampleFrequency() != 44100
              || output.getChannelCount() != 2) {
            throw new com.mindtherobot.libs.mpg.DecoderException("mono or non-44100 MP3 not supported");
          }

          short[] pcm = output.getBuffer();
          for (short s : pcm) {
            outStream.write(s & 0xff);
            outStream.write((s >> 8 ) & 0xff);
          }
        }

        if (totalMs >= (startMs + maxMs)) {
          done = true;
        }
      }
      bitstream.closeFrame();
    }

    return outStream.toByteArray();
  } catch (BitstreamException e) {
    throw new IOException("Bitstream error: " + e);
  } catch (DecoderException e) {
    Log.w(TAG, "Decoder error", e);
    throw new com.mindtherobot.libs.mpg.DecoderException(e);
  } finally {
    IOUtils.safeClose(inputStream);    
  }
}

It is noteworthy that the AudioTrack was not designed for the purpose of uploading audio files. The ideal to load audio compressed files (such as mp3 and ogg, for example) would be the classes Mediaplayer or Soundpoll, as can be seen in answers to that question soen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.