public void init(MediaFormat format) {
        Log.d(TAG, "init");

        boolean playing = false;

        if(isInitialized()) {
            if(!checkIfReinitializationRequired(format)) {
                // Set new format that equals the old one (in case we compare references somewhere)
                mAudioFormat = format;
                return;
            }

            playing = isPlaying();
            pause();
            stopAndRelease(false);
        } else {
            // deferred creation of the audio thread until its first use
            mAudioThread = new AudioThread();
            mAudioThread.setPaused(true);
            mAudioThread.start();
        }

        mAudioFormat = format;

        int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
        int bytesPerSample = 2;
        mFrameSize = bytesPerSample * channelCount;
        mSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);

        int channelConfig = AudioFormat.CHANNEL_OUT_DEFAULT;
        switch(channelCount) {
            case 1:
                channelConfig = AudioFormat.CHANNEL_OUT_MONO;
                break;
            case 2:
                channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
                break;
            case 4:
                channelConfig = AudioFormat.CHANNEL_OUT_QUAD;
                break;
            case 6:
                channelConfig = AudioFormat.CHANNEL_OUT_5POINT1;
                break;
            case 8:
                channelConfig = AudioFormat.CHANNEL_OUT_7POINT1;
        }

        mPlaybackBufferSize = mFrameChunkSize * channelCount;

        mAudioTrack = new AudioTrack(
                mAudioStreamType,
                mSampleRate,
                channelConfig,
                AudioFormat.ENCODING_PCM_16BIT,
                mPlaybackBufferSize, // at least twice the size to enable double buffering (according to docs)
                AudioTrack.MODE_STREAM, mAudioSessionId);

        if(mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
            stopAndRelease();
            throw new IllegalStateException("audio track init failed");
        }

        mAudioSessionId = mAudioTrack.getAudioSessionId();
        mAudioStreamType = mAudioTrack.getStreamType();
        setStereoVolume(mVolumeLeft, mVolumeRight);
        mPresentationTimeOffsetUs = PTS_NOT_SET;

        if(playing) {
            play();
        }
    }
Initializes or reinitializes the audio track with the supplied format for playback
while keeping the playstate. Keeps the current configuration and skips reinitialization
if the new format is the same as the current format.
MediaPlayer/src/main/java/net/protyposis/android/mediaplayer/AudioPlayback.java