手里一个项目,用到 Visualizer 采集MediaPlayer播放音乐的频谱信息,大部分手机测试正常,但是在三星S8上出现用手机自带扬声器播放音乐正常 但是如果插上耳机出现,onDataCaptureListener 的 onWaveFormDataCapture 方法只会调用2次,之后不会触发onWaveFormDataCapture,无法拿到数据。求大神解决。
// 初始化音谱采集器
mVisualizer = new Visualizer(player.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[0]);
mVisualizer.setDataCaptureListener(onDataCaptureListener, Visualizer.getMaxCaptureRate(), true, false);
private Visualizer.OnDataCaptureListener onDataCaptureListener = new Visualizer.OnDataCaptureListener() {
@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int i) {
int v = average(bytes);
int s = (int) (v / 128.00 * 90 + 10);
s5 = Integer.toHexString(s);
if (s5.length() == 1) {
s5 = "0" + s5;
}
try {
if (!player.isPlaying()) {
String ins = Globals.MODE_f0 + "000000FF";
mLeProxy.send(mLeProxy.getConnectedDevices().get(0).getAddress(), DataUtil.hexToByteArray(ins), false);
} else {
String ins = Globals.MODE_f0 + Globals.s4 + s5 + "00FF";
mLeProxy.send(mLeProxy.getConnectedDevices().get(0).getAddress(), DataUtil.hexToByteArray(ins), false);
}
} catch (Exception e) {
}
}
@Override
public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int i) {
}
};