1

audiorecordを使用して送信音声データをローカルサーバーにすぐに送信しようとしています.音声を録音してSDカードに保存することはできますが、音声音声をバッファに保存し、HTTP POSTを使用してサーバーにすぐに送信します. 打たれた..どうすれば進めますか?私はこのアプリをICS、つまりAndroid 4.0.3バージョンで作成しています。

4

1 に答える 1

2

解決策を得ました..音声データをjsonarrayとしてサーバーに送信し、jsonオブジェクトとして受け取ります。jsonobject を短い配列に変換し、オーディオトラックに書き込み、再生します。これがサンプルコードの動作です。

 public void SendAudio() {

    String LOG_TAG = null;
    long SAMPLE_INTERVAL = 1;

    Log.e(LOG_TAG, "start send thread, thread id: ");

    int bytes_read = 0;
    int bytes_count = 0;

    br = br % 5;
    br++;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpClient httpclient_recv = new DefaultHttpClient();
        // System.out.println("mmdata--------- " + value);
        HttpPost httppost = new HttpPost("http://192.168.1.39/call");
        HttpPost httppost_recv = new HttpPost("http://192.168.1.39/ping");
        // Unix time stamp
        long unixTime = System.currentTimeMillis();
        String timestamp = String.valueOf(unixTime);

        // Json Format
        JSONObject holder = new JSONObject();
        JSONObject holder_recv = new JSONObject();
        JSONArray jArray = new JSONArray();
        try {

            holder.put("UserId", "1");
            holder.put("TimeStamp", timestamp);
            holder.put("SessionId", "1");

            Queue<byte[]> qArray = new LinkedList<byte[]>();
            qArray.add(bData);

            byte[] tmparr = qArray.poll();
            for (int i = 0; i < tmparr.length; i++) {
                jArray.put(i, tmparr[i]);
            }
            System.out.println("send audio -- " + jArray);
            holder.put("MMData", jArray.toString());
            System.out.println("JSON -- " + holder.toString());

            holder_recv.put("UserId", "1");
            holder_recv.put("TimeStamp", timestamp);
            holder_recv.put("SeqNo", "100");
            holder_recv.put("SessionId", "0");

        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        System.out.println("time " + timestamp);
        try {

            StringEntity se = new StringEntity(holder.toString());
            StringEntity se_recv = new StringEntity(holder_recv.toString());

            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            se_recv.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));

            httppost.setEntity(se);
            httppost_recv.setEntity(se_recv);
            HttpResponse response = httpclient.execute(httppost);
            HttpResponse response_recv = httpclient_recv
                    .execute(httppost_recv);

            if (response_recv != null) {
                t = EntityUtils.toString(response_recv.getEntity());
                System.out.println("after response -- " + t);
                Queue<String> qe = new LinkedList<String>();
                qe.add(t);

                t = null;
                System.out.println("on play side ...1 ");
                try {
                    System.out.println("on play side ...2 ");
                    JSONObject get = new JSONObject(qe.poll());

                    // JSONArray jArray_recv = get.getJSONArray("MMData");

                    String mmdata = (String) get.get("MMData");
                    JSONObject temp2 = new JSONObject("{ \"MMData\" : "
                            + mmdata + "}");
                    JSONArray jArray_recv = temp2.getJSONArray("MMData");
                    System.out.println("jsonarray -- " + jArray_recv);
                    // String timeData = (String) get.get("TimeStamp");
                    String userData = (String) get.get("UserId");
                    // String sessionId = (String) get.get("TimeStamp");
                    System.out.println("on play side ...3 ");
                    // if (Long.valueOf(timeData) > ts)
                    {
                        // ts = Long.valueOf(timeData);
                        System.out.println("on play side ...4 ");
                        if (at != null) {
                            System.out.println("on play side ...5 ");
                            byte[] shortArray = new byte[jArray_recv
                                    .length()];
                            for (int i = 0; i < jArray_recv.length(); i++) {
                                shortArray[i] = (byte) jArray_recv
                                        .getInt(i);
                            }
                            jArray_recv = null;
                            System.out.println("recv audio -- " + br
                                    + " ----" + shortArray);
                            byte[] temp = shortArray;
                            shortArray = null;

                            byte[] bArray = temp;
                            temp = null;
                            byte[][] newbarray = new byte[5][1024];
                            newbarray[br - 1] = bArray;
                            byte[] sbayyay = new byte[1024 * 5];
                            System.arraycopy(bArray, 0, sbayyay,
                                    (br - 1) * 1024, bArray.length);
                            if (br == 5) {
                                for (int i = 0; i < 5; i++) {
                                    System.out.println("in for---" + i
                                            + "----" + sbayyay.length);
                                    // System.arraycopy(newbarray[i], 0,
                                    // sbayyay, i * 1024,
                                    // bArray.length);
                                }
                                System.out.println("bsbsbsbs --- "
                                        + new String(sbayyay));
                                at.write(sbayyay, 0, sbayyay.length);
                                at.play();

                            }

                        }

                        else {
                            Log.d("Audio",
                                    "audio track is not initialised ");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Exception");
            e.printStackTrace();
        }
        bytes_count += bytes_read;
        Log.d(LOG_TAG, "bytes_count : " + bytes_count);
        Thread.sleep(SAMPLE_INTERVAL, 0);
    } catch (InterruptedException ie) {
        Log.e(LOG_TAG, "InterruptedException");
    }

}
于 2012-09-22T10:17:57.273 に答える