Created
October 1, 2012 15:23
-
-
Save JavaDeveloper/3812477 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.ByteBuffer; | |
import android.content.Context; | |
import android.media.AudioFormat; | |
import android.media.AudioManager; | |
import android.media.AudioTrack; | |
import android.media.MediaFormat; | |
import android.net.Uri; | |
class AudioTrackDecoder extends AbstractTrackDecoder { | |
private static final String TAG = AudioTrackDecoder.class.getSimpleName(); | |
private static final String AUDIO_PREFIX = "audio/"; | |
private static final int AUDIO_TRACK_BUFFER_SIZE = 2048; | |
private AudioTrack audioTrack; | |
public AudioTrackDecoder(String path) { | |
super(path, AUDIO_PREFIX, null/* surface */); | |
initilizeAudio(); | |
} | |
private void initilizeAudio() { | |
int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE); | |
int chanelCfg = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT); | |
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, | |
chanelCfg /*AudioFormat.CHANNEL_CONFIGURATION_MONO*/, AudioFormat.ENCODING_PCM_16BIT, | |
AUDIO_TRACK_BUFFER_SIZE, AudioTrack.MODE_STREAM); | |
audioTrack.play(); | |
} | |
@Override | |
protected void processDecodedData() { | |
if (outputBufferIndex < 0) { | |
return; | |
} | |
ByteBuffer buf = outputBuffers[outputBufferIndex]; | |
final byte[] chunk = new byte[bufferInfo.size]; | |
buf.get(chunk); | |
buf.clear(); | |
if (chunk.length > 0) { | |
audioTrack.write(chunk, 0, chunk.length); | |
} | |
codec.releaseOutputBuffer(outputBufferIndex, false /* render */); | |
} | |
@Override | |
public void cleanup() { | |
audioTrack.stop(); | |
audioTrack.release(); | |
audioTrack = null; | |
super.cleanup(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment