关于安卓visualizer的用法
看别人显示播放wav文件显示频谱写的代码都是断断续续的,在这里我贴了完整的代码,给有需要的人做参考,显示频谱还没有完成,不知道怎么弄,已经可以得到byte[] fft数据了,参考别人的写法也可以开方取模了,但是就是耗在了byte[]转doule[]上了,因为我用achartEngine画图的,所以现在还没有解决办法,有谁知道的,请在给我留言,谢谢。下面上代码,布局文件就不贴了,很简单的就是两个按钮
package com.example.visualizer; import android.app.Activity; import android.media.MediaPlayer; import android.media.audiofx.Visualizer; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private MediaPlayer mediaPlayer; private Visualizer visualizer; Button playButton, stopButton; Uri pathUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); playButton = (Button) findViewById(R.id.button1); stopButton = (Button) findViewById(R.id.button2); playButton.setOnClickListener(listener); stopButton.setOnClickListener(listener); pathUri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/heartBit.wav"); System.out.println(pathUri); mediaPlayer = MediaPlayer.create(this, pathUri); visualizer = new Visualizer(mediaPlayer.getAudioSessionId()); visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]); System.out.println(Visualizer.getCaptureSizeRange()[1]); visualizer.setDataCaptureListener( new Visualizer.OnDataCaptureListener() { @Override public void onWaveFormDataCapture(Visualizer arg0, byte[] arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onFftDataCapture(Visualizer arg0, byte[] fft, int arg2) {//返回的经过FFT处理后的数据 // TODO Auto-generated method stub try { System.out.println(DataFix.getHexString(fft)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, Visualizer.getMaxCaptureRate() / 2, false, true); visualizer.setEnabled(true);//这步不能忘记 mediaPlayer .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {//监听器 @Override public void onCompletion(MediaPlayer arg0) { // TODO Auto-generated method stub visualizer.setEnabled(false); } }); } View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.button1: mediaPlayer.start(); break; case R.id.button2: mediaPlayer.pause(); break; default: break; } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }开方取模的处理方法,参考别人的代码:
byte[] model = new byte[fft.length / 2 + 1]; model[0] = (byte) Math.abs(fft[0]); for (int i = 2, j = 1; j < mSpectrumNum;) { model[j] = (byte) Math.hypot(fft[i], fft[i + 1]); i += 2; j++; }到这里返回的还是byte[]数组,话说java下的转型相当麻烦,还不知道怎么解决!