android均衡器

调节高音和低音

运行效果图:

android均衡器_第1张图片

package com.terry.AudioFx;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.audiofx.Equalizer;
import android.media.audiofx.Visualizer;
import android.media.audiofx.Visualizer.OnDataCaptureListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class AudioFxActivity extends Activity
{

	private static final String TAG = "AudioFxActivity";

	private static final float VISUALIZER_HEIGHT_DIP = 50f;

	private MediaPlayer mMediaPlayer;
	private Visualizer mVisualizer;
	private Equalizer mEqualizer; // 均衡器

	private LinearLayout mLayout;
	VisualizerView mVisualizerView;
	private TextView mStatusTextView;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		mStatusTextView = new TextView(this);
		mLayout = new LinearLayout(this);
		mLayout.setOrientation(LinearLayout.VERTICAL);
		mLayout.addView(mStatusTextView);
		setContentView(mLayout);

		mMediaPlayer = MediaPlayer.create(this, R.raw.kongcheng);
		if( null == mMediaPlayer )  
			return ;  


		setupVisualizerFxAndUi();
		setupEqualizeFxAndUi();
		// Make sure the visualizer is enabled only when you actually want to receive data, and  
		// when it makes sense to receive data.  
		mVisualizer.setEnabled(true);
		mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
		{

			public void onCompletion(MediaPlayer mp)
			{
				// TODO Auto-generated method stub
				mVisualizer.setEnabled(false);
			}
		});

		mMediaPlayer.start();
		mStatusTextView.setText("播放中。。。");
	}

	/**
	 * 通过mMediaPlayer返回的AudioSessionId创建一个优先级为0均衡器对象 并且通过频谱生成相应的UI和对应的事件
	 */
	private void setupEqualizeFxAndUi()
	{
		mEqualizer = new Equalizer(0, mMediaPlayer.getAudioSessionId());

		mEqualizer.setEnabled(true);// 启用均衡器
		TextView eqTextView = new TextView(this);
		eqTextView.setText("均衡器:");
		mLayout.addView(eqTextView);

		// 通过均衡器得到其支持的频谱引擎
		short bands = mEqualizer.getNumberOfBands();

		// getBandLevelRange 是一个数组,返回一组频谱等级数组,
		// 第一个下标为最低的限度范围
		// 第二个下标为最大的上限,依次取出
		final short minEqualizer = mEqualizer.getBandLevelRange()[0];
		final short maxEqualizer = mEqualizer.getBandLevelRange()[1];
		for (short i = 0; i < bands; i++)
		{
			final short band = i;

			TextView freqTextView = new TextView(this);
			freqTextView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));

			freqTextView.setGravity(Gravity.CENTER_HORIZONTAL);

			// 取出中心频率
			freqTextView
			.setText((mEqualizer.getCenterFreq(band) / 1000) + "HZ");
			mLayout.addView(freqTextView);



			LinearLayout row = new LinearLayout(this);
			row.setOrientation(LinearLayout.HORIZONTAL);

			TextView minDbTextView = new TextView(this);
			minDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));

			minDbTextView.setText((minEqualizer / 100) + " dB");



			TextView maxDbTextView = new TextView(this);
			maxDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
			maxDbTextView.setText((maxEqualizer / 100) + " dB");


			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);

			layoutParams.weight = 1;

			SeekBar seekbar = new SeekBar(this);
			seekbar.setLayoutParams(layoutParams);
			seekbar.setMax(maxEqualizer - minEqualizer);
			seekbar.setProgress(mEqualizer.getBandLevel(band));

			seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
			{

				public void onStopTrackingTouch(SeekBar seekBar)
				{
				}

				public void onStartTrackingTouch(SeekBar seekBar)
				{
				}

				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser)
				{
					// TODO Auto-generated method stub
					mEqualizer.setBandLevel(band,
							(short) (progress + minEqualizer));
				}
			});
			row.addView(minDbTextView);
			row.addView(seekbar);
			row.addView(maxDbTextView);

			mLayout.addView(row);
		}

	}

	/**
	 * 生成一个VisualizerView对象,使音频频谱的波段能够反映到 VisualizerView上
	 */
	private void setupVisualizerFxAndUi()
	{
		mVisualizerView = new VisualizerView(this);
		mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				(int) (VISUALIZER_HEIGHT_DIP * getResources()
						.getDisplayMetrics().density)));
		mLayout.addView(mVisualizerView);

		mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());
		// 参数内必须是2的位数
		//设置每次捕获频谱的大小,音乐在播放中的时候采集的数据的大小或者说是采集的精度吧,我的理解,而且getCaptureSizeRange()
		//		所返回的数组里面就两个值 .文档里说数组[0]是最小值(128),数组[1]是最大值(1024)。
		mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
		// 设置允许波形表示,并且捕获它
		mVisualizer.setDataCaptureListener(new OnDataCaptureListener()
		{
			//接下来就好理解了设置一个监听器来监听不断而来的所采集的数据。
			//一共有4个参数,第一个是监听者,第二个单位是毫赫兹,表示的是采集的频率,第三个是是否采集波形,第四个是是否采集频率

			public void onWaveFormDataCapture(Visualizer visualizer,
					byte[] waveform, int samplingRate)
			{
				// TODO Auto-generated method stub
				mVisualizerView.updateVisualizer(waveform);
				for (int i=0;i

 

你可能感兴趣的:(android,学习)