package com.android.audiomodem;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.AsyncTask;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;

public class Sender extends AsyncTask<String, Double, Void> {

    static class OutputBuffer implements jmodem.OutputSampleStream {

        final ByteArrayOutputStream out;
        final DataOutputStream stream;

        public OutputBuffer() {
            out = new ByteArrayOutputStream();
            stream = new DataOutputStream(out);
        }

        @Override
        public void write(double value) throws IOException {
            short sample = (short) (jmodem.Config.scalingFactor * value);
            stream.writeShort(sample);
        }

        public short[] samples() {
            ShortBuffer b = ByteBuffer.wrap(out.toByteArray()).asShortBuffer();
            short[] result = new short[b.capacity()];
            b.get(result);
            return result;
        }
    }

    final static int sampleRate = jmodem.Config.sampleRate;

    final static int streamType = AudioManager.STREAM_MUSIC;
    final static String TAG = "Sender";

    @Override
    protected Void doInBackground(String... params) {

        final int chanFormat = AudioFormat.CHANNEL_OUT_MONO;
        final int encoding = AudioFormat.ENCODING_PCM_16BIT;
        final int mode = AudioTrack.MODE_STATIC;
        final int bufSize = AudioTrack.getMinBufferSize(sampleRate, chanFormat, encoding);

        OutputBuffer buf = new OutputBuffer();
        final byte[] data = params[0].getBytes();

        jmodem.Sender send = new jmodem.Sender(buf);
        try {
            send.writePrefix();
            send.writeTraining();
            send.writeData(data, data.length);
            send.writeEOF();
        } catch (IOException e) {
            Log.e(TAG, "sending data failed", e);
            return null;
        }

        short[] samples = buf.samples();

        AudioTrack dst = new AudioTrack(
                streamType,
                sampleRate,
                chanFormat,
                encoding,
                Math.max(samples.length, bufSize) * 2,
                mode
        );
        int n = dst.write(samples, 0, samples.length);
        double duration = ((double) n) / sampleRate;
        Log.d(TAG, String.format("playing %d samples (%f seconds)", n, duration));

        dst.play();
        try {
            final double dt = 0.1;
            for (double t = 0; t < duration; t += dt) {
                publishProgress(t / duration);
                Thread.sleep((long) (dt * 1000));
            }
        } catch (InterruptedException e) {
            // nothing to do
        }
        publishProgress(1.0);
        dst.stop();
        dst.release();
        return null;
    }

}
@@ -183,16 +183,89 @@
 l.Log;%0A%0A
+import java.io.BufferedInputStream;%0Aimport java.io.ByteArrayInputStream;%0A
 import j
@@ -1813,206 +1813,79 @@
     
-jmodem.Sender send = new jmodem.Sender(buf);%0A        try %7B%0A            send.writePrefix();%0A            send.writeTraining();%0A            send.writeData(data, data.length);%0A            send.writeEOF(
+try %7B%0A            jmodem.Sender.run(new ByteArrayInputStream(data), buf
 );%0A 

use public MODEM API