安卓音樂頻譜

leo77發表於2018-09-14
  • 最近比較閒,在公司聽歌刷網頁,發現一首歌 
  • [Trash Moment ](https://y.qq.com/n/yqq/song/000C8VXK4GCXsX.html),
  •  瞬間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);
                }
            }
        });

    }

}複製程式碼


<?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>複製程式碼


相關文章