我想可视化一个声音文件,但是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();
}
发布于 2015-02-11 20:21:06
由于对原语进行了签名,Java编译器将阻止您为一个字节分配高于+127的值(或小于-128的值)。
然而,为了实现这一点,没有什么可以阻止你向下转换为int (或short):
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.
https://stackoverflow.com/questions/28453737
复制相似问题