Visualizer始终返回-128

时间:2015-02-11 11:54:57

标签: java android

我想要显示声音文件,但方法visualizer.getWaveForm(data)始终返回-128

你现在有什么不对吗?

try {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().toString()+ "/test.ogg");

        int audioSessionID = mediaPlayer.getAudioSessionId();
        Visualizer visualizer = new Visualizer(audioSessionID);

        visualizer.setEnabled(true);
        byte[] data = new byte[visualizer.getCaptureSize()];
        visualizer.getWaveForm(data);
        for(int i=0;i<data.length;i++){
            Log.d("d",Integer.toString(data[i]));
        }
    } catch (IllegalArgumentException e) {
        Log.d("d","p1");
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        Log.d("d","p2");
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        Log.d("d","p3");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("d","p4");
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

当原语被签名时,Java编译器将阻止您将高于+127的值分配给一个字节(或低于-128)。

然而,为了达到这个目的,没有什么可以阻止你向下转换为int(或简称):

int i = 200;
byte b = (byte)200;

// Will print a negative value but you could *still choose to interpret* this as +200.
System.err.println(b); 

// "Upcast" to short in order to easily view / interpret as a positive value.
// You would typically do this *within* the method that expected an unsigned byte.
short s = b & 0xFF;
System.err.println(s); // Will print a positive value.