繁体   English   中英

如何将音频文件(mp3或WAV)从应用程序发送到服务器?

[英]How to send audio file (mp3 or WAV) from app to server?

我正在尝试将mp3文件从应用程序发送到我制作的服务器,该文件是我的应用程序执行的录音。 一直在使用凌空来发送仅包含字符串的请求,但是现在我需要发送文件。 一直试图将文件转换为字节数组并发送,但是似乎从文件转换为字节数组是不正确的。

private byte[] fileToBytes() {
    byte[] bytes = null;
    File audioFile = new File(mPathSave);
    try {

        bytes = Files.readAllBytes(audioFile.toPath());
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bytes;
}

private void isCorrectAnswer(MediaRecorder iRecorder, final Button iButton) {
    String url = "myUrl";
    File file = new File(mPathSave);
    try {
        InputStream inFile = new FileInputStream(mPathSave);
        byte[] bytes = fileToBytes();

        JSONObject jsonBody = new JSONObject();
        jsonBody.put("answer", mQuestion.GetmAnswer());
        jsonBody.put("email", "email@email.com");
        jsonBody.put("audio_file", bytes);
        final RequestQueue queue = Volley.newRequestQueue(this);
        RequestFuture<JSONObject> future = RequestFuture.newFuture();
        final JsonObjectRequest jsonRequest =
                new JsonObjectRequest(Request.Method.POST, url, jsonBody,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {

                                iButton.setText(response.toString());
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.print("ERROR!");
                    }
                });
        queue.add(jsonRequest);


    } catch (IOException e) {
        e.printStackTrace();
    }
}

更新 :文件正在被转换为此:

[B @ 5bc86d4

但是我需要将其转换为这样的东西:

b'\\ x00 \\ x00 \\ x00 \\ x18ftypmp42 \\ x00 \\ x00 \\ x00 \\ x00isommp42 \\ x00 \\ x00 \\ x02 \\ xf6moov \\ x00 \\ x00 \\ x00lmxvlmd \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ xd9X \\ x87 \\ xf9 \\ xd9X \\ x87 \\ xf9 \\ x00 \\ x00 \\ x03 \\ xe8 \\ x00 \\ x00 \\ n \\ x00 \\ x00 \\ x01 \\ x00 \\ x00 \\ x01 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x01 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x01 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 @ \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x02 \\ x00 \\ x00 \\ x00wmeta \\ x00 \\ x00 \\ x00!hdlr \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00mdta \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 + keys \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x01 \\ x00 \\ x00 \\ x00 \\ x1bmdtacom.android.version \\ x00 \\ x00 \\ x00#ilst \\ x00 \\ x00 \\ x00 \\ x1b \\ x00 \\ x00 \\ x00 \\ x01 \\ x00 \\ x00 \\ x00 \\ x13data \\ x00 \\ x00 \\ x00 \\…

(确实很长,但是您可以看到区别...)

这样使用多部分请求

private SimpleMultiPartRequest smr;
private RequestQueue mRequestQueue;

主要活动:

mRequestQueue = Volley.newRequestQueue(this);


 private void imageUpload(final String imagePath) {

    smr = new SimpleMultiPartRequest(Request.Method.POST, Globalvariable.BASE_URL + "api/profile/image",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("Response", response);

                    try {
                        JSONObject jsonObject = new JSONObject(response);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }



                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(com.android.volley.error.VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });


    smr.addFile("Image", imagePath);
    smr.addMultipartParam("Id","multipart/form-data",userid);

    //MyApplication.getInstance().addToRequestQueue(smr);
    mRequestQueue.add(smr);
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM