在短到Android的字节数组 [英] short to byte array in android

查看:186
本文介绍了在短到Android的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于种种原因,我需要一个音频采样转换为用于网络传输的byte []。我发现short2bytearray功能

For some reasons, I need to convert the samples of a audio into byte[] for network transmission. I found a function for short2bytearray

public static byte[] short2byte(short in) {
        byte[] ret = new byte[2];
        ret[0] = (byte) (in & 0xff);
        ret[1] = (byte) ((in >> 8) & 0xff);
        return ret;
}

因为音频继续播放以及样本继续服用,所以我保存的样品到一个HashMap,然后再打开一个线程来处理此地图。

because the audio keeps playing and samples are keep taking, so I save samples into a HashMap first and then open a thread to process this map.

private void convert() {
        Thread conv = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                // while (needConver) {
                Iterator<Entry<Integer, short[]>> iter = sampleBuffer
                        .entrySet().iterator();
                Log.i("Converting", "enter");
                if (iter.hasNext()) {
                    Entry<Integer, short[]> entry = iter.next();
                    //stops in here
                    byte[] converted = new byte[entry.getValue().length * 2];
                    for (short in : entry.getValue()) {
                        Admin.combineByteArray(converted, Admin.short2byte(in));
                    }
                    convertedBuffer.put(entry.getKey(), converted);
                    sampleBuffer.remove(entry.getKey());
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // }
            }

        });
        conv.start();
    }

线程只会造成一次。我想它也保持跟踪sampleBuffer并转换数据存储在其中。然而,当我运行的对话功能 Admin.short2byte(中),它会输出大量的信息,如

The thread will only create once. I want it too keep tracking the sampleBuffer and convert data that stores in it. However, when I run the conversation function Admin.short2byte(in), it will output lots of information like

`03-22 14:52:48.004: D/dalvikvm(24194): GC_FOR_ALLOC freed 4096K, 50% free 8517K/16839K, paused 13ms, total 13ms`

请你告诉我为什么,以及如何解决这一问题?谢谢

could you please tell me why and how to fix this? thank you

推荐答案

有几个问题你code。首先,你,而你是通过它遍历从本 sampleBuffer 地图元素。我很惊讶你没有得到与该异常。其次,你的建设成果阵列的方法非常低效,产生大量垃圾。你会过得更好使用 java.nio.ByteBuffer中,消除你的 short2byte 方法。我会写你的code是这样的:

There are several problems with your code. First, you are removing elements from the sampleBuffer map while you are iterating through it. I'm surprised you aren't getting an exception with that. Second, your method of building the results array is extremely inefficient and generates lots of garbage. You would be better off using a java.nio.ByteBuffer and eliminate your short2byte method. I would write your code like this:

private void convert() {
    new Thread() {
        @Override
        public void run() {
            for (Entry<Integer, short[]> entry : sampleBuffer.entrySet()) {
                short[] values = entry.getValue();
                byte[] converted = new byte[values.length * 2];
                ByteBuffer buff = ByteBuffer.wrap(converted);
                for (short in : values) {
                    buff.putShort(in);
                }
                convertedBuffer.put(entry.getKey(), converted);
            }
        }

    }.start();
}

这篇关于在短到Android的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆