接收时音频文件的质量下降

时间:2014-06-12 10:52:53

标签: android sockets audio

在我的应用程序中,我使用Sockets将音频文件从一个设备发送到另一个设备。我确信我发送的是最优质的音频文件(更高的采样率)。但是虽然接收采样率非常差,我甚至听不到音频。我该怎么办?

我的发送代码:

try {
    FileInputStream audio_fis = new FileInputStream(file);
    DataInputStream audio_dis = new DataInputStream(audio_fis);
    byte[] mybytearray = new byte[512];
    int audio_length = 0;
    os = socket.getOutputStream();

    for (int j = 0; j < nosofpackets + 1; j++) {
        audio_length = audio_dis.read(mybytearray, 0, 512);
        if (audio_length > -1) {
            Thread.sleep(100);
            os.write(mybytearray, 0, 512);
            os.flush();
        } else ;
        /*{
     PrintWriter out = new PrintWriter(new BufferedWriter(
     new OutputStreamWriter(socket.getOutputStream())),
     true);
     out.println("EOF");
     break;
     }*/
        System.out.println("File sending...");
    }
    Thread.sleep(100);
    audio_dis.close();
    audio_fis.close();
    Log.d("", "sending completed");

} catch (FileNotFoundException e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), "File NotFound", Toast.LENGTH_SHORT).show();

} catch (IOException e) {
    e.printStackTrace();
    new Thread(new ClientThread()).start();
    System.out.println("123");
}

我的接收代码:

InputStream socket_is = null;
try {
    socket_is = mclientSocket.getInputStream();
    audio_fos = new FileOutputStream(audio_file.getAbsoluteFile());
    audio_dos = new DataOutputStream(audio_fos);
    int length;
    for (int i = 0; i < nosofpackets + 1; i++) {
        mhandler.post(new Runnable() {

            @Override
            public void run() {

                Toast.makeText(getApplicationContext(), "file receiving..." + nosofpackets,
                        Toast.LENGTH_SHORT).show();
            }
        });

        length = socket_is.read(mybytearray, 0, mybytearray.length);
        if (length > -1) {
            audio_dos.write(mybytearray, 0, mybytearray.length);
            Thread.sleep(10);

        } else ;
        Thread.sleep(100);
        //socket_is.read(mybytearray);
        //audio_dos.write(mybytearray);
    }
    Thread.sleep(100);
    audio_dos.close();
    audio_fos.close();
} catch (IOException e) {

    mhandler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "Socket Exception..",
                    Toast.LENGTH_SHORT).show();
        }
    });
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

0 个答案:

没有答案