- 最近比较闲,在公司听歌刷网页,发现一首歌
- [Trash Moment (Explicit)](y.qq.com/n/yqq/song/…), 瞬间high起来,起来,来...
- [现场MV](https://www.youtube.com/watch?v=_5bK3ABjg_s)
- 然后就做了个音乐频谱,没几行代码,增加自己听歌的舒适度...
- 如图:
- 代码:
package cn.leo.visualizer;
import android.graphics.Outline;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewOutlineProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mPlayer;
private Visualizer mVisualizer;
private List<View> viewList =new ArrayList<>();
private ConstraintLayout root;
private int ViewSize;
private int num=48;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlayer = MediaPlayer.create(this,R.raw.tm);
mVisualizer = new Visualizer(mPlayer.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
@Override
public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {
byte[] model = new byte[fft.length / 2 + 1];//0-511
model[0] = (byte) Math.abs(fft[0]);
for (int i = 2, j = 1; j < model.length-1;)
{
model[j] = (byte) Math.hypot(fft[i], fft[i + 1]);
i += 2;
j++;
}
for(int i=0;i<viewList.size();i++){
int value = Math.abs(model[i]);
if(value<1){
value=1;
}
/** 效果一:柱状图*/
// viewList.get(i).getLayoutParams().height = ViewSize*(value+1)/3;
// viewList.get(i).requestLayout();
/**效果二:圆球跳动*/
viewList.get(i).setY(ViewSize*value/3);
}
}
@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate) {
}
}, Visualizer.getMaxCaptureRate()/2, false, true);
mVisualizer.setEnabled(true);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mPlayer.start();
}
});
mPlayer.start();
// 插入子view
root =findViewById(R.id.rootview);
root.post(new Runnable() {
@Override
public void run() {
ViewSize = root.getWidth()/num;
for(int i=0;i<num;i++){
View v =new View(MainActivity.this);
v.setLayoutParams(new ConstraintLayout.LayoutParams(ViewSize,ViewSize));
v.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0,view.getWidth(), view.getHeight(), view.getWidth()/2);
outline.canClip();
}
});
v.setClipToOutline(true);
int ranColor = 0xff000000 | new Random().nextInt(0x00ffffff);
v.setBackgroundColor(ranColor);
root.addView(v,i);
int W = i*ViewSize;
v.setX(W);
v.setY(ViewSize);
viewList.add(v);
}
}
});
}
}复制代码
扫描二维码关注公众号,回复:
3172885 查看本文章
![](/qrcode.jpg)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootview"
android:background="#030303"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>复制代码