1.android如何做到播放音乐时动态波形
2.android平台的音音乐音乐播放器,播放音乐(这应该是乐播一个很耗时的操作)能不能在子线程中实现?
android如何做到播放音乐时动态波形
1. 确保MediaPlayer正确创建并播放音乐。
2. 检查Visualizer和Equalizer的放源实例化是否在正确的生命周期内。
3. 确保在Activity暂停时正确释放Visualizer、码a码房卡网页版源码Equalizer和MediaPlayer资源。播放
4. 修改VisualizerView的器源燕窝之溯源码onDraw方法,确保正确绘制波形和频谱。音音乐
5. 添加必要的乐播错误处理和日志输出,以便调试。放源
修改后的码a码代码如下:
```java
package com.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.audiofx.Equalizer;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class AudioFxActivity extends Activity {
private static final String TAG = "AudioFxActivity";
private static final float VISUALIZER_HEIGHT_DIP = f;
private MediaPlayer mMediaPlayer;
private Visualizer mVisualizer;
private Equalizer mEqualizer;
private LinearLayout mLinearLayout;
private VisualizerView mVisualizerView;
private TextView mStatusTextView;
private TextView mInfoView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mStatusTextView = new TextView(this);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mLinearLayout.addView(mStatusTextView);
setContentView(mLinearLayout);
// Create the MediaPlayer
try {
mMediaPlayer = MediaPlayer.create(this, R.raw.my_life);
} catch (Exception e) {
Log.e(TAG, "Error creating MediaPlayer", e);
finish();
return;
}
Log.d(TAG, "MediaPlayer audio session ID: " + mMediaPlayer.getAudioSessionId());
setupVisualizerFxAndUI();
setupEqualizerFxAndUI();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mStatusTextView.setText("Playing music...");
}
private void setupEqualizerFxAndUI() {
// Create the Equalizer object (an AudioEffect subclass) and attach it
// to our media player, with a default priority (0).
mEqualizer = new Equalizer(0, mMediaPlayer.getAudioSessionId());
mEqualizer.setEnabled(true);
TextView eqTextView = new TextView(this);
eqTextView.setText("Equalizer:");
mLinearLayout.addView(eqTextView);
short bands = mEqualizer.getNumberOfBands();
final short minEQLevel = mEqualizer.getBandLevelRange()[0];
final short maxEQLevel = 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.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
freqTextView.setGravity(Gravity.CENTER_HORIZONTAL);
freqTextView.setText((mEqualizer.getCenterFreq(band) / ) + " Hz");
mLinearLayout.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((minEQLevel / ) + " dB");
row.addView(minDbTextView);
TextView maxDbTextView = new TextView(this);
maxDbTextView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
maxDbTextView.setText((maxEQLevel / ) + " dB");
row.addView(maxDbTextView);
SeekBar bar = new SeekBar(this);
bar.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(mEqualizer.getBandLevel(band));
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mEqualizer.setBandLevel(band, (short) (progress + minEQLevel));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
row.addView(bar);
mLinearLayout.addView(row);
}
}
private void setupVisualizerFxAndUI() {
mVisualizerView = new VisualizerView(this);
mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
(int) (VISUALIZER_HEIGHT_DIP * getResources().getDisplayMetrics().density)));
mLinearLayout.addView(mVisualizerView);
mInfoView = new TextView(this);
String infoStr = "";
int[] csr = Visualizer.getCaptureSizeRange();
if (csr != null) {
String csrStr = "CaptureSizeRange: ";
for (int i = 0; i < csr.length; i++) {
csrStr += csr[i];
csrStr += " ";
}
infoStr += csrStr;
}
final int maxCR = Visualizer.getMaxCaptureRate();
infoStr = infoStr + "\nMaxCaptureRate: " + maxCR;
mInfoView.setText(infoStr);
mLinearLayout.addView(mInfoView);
mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());
mVisualizer.setCaptureSize();
mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
mVisualizerView.updateVisualizer(bytes);
}
@Override
public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {
mVisualizerView.updateVisualizer(fft);
}
}, maxCR / 2, false, true);
}
@Override
protected void onPause() {
super.onPause();
android平台的音乐播放器,播放音乐(这应该是播放一个很耗时的操作)能不能在子线程中实现?
Android平台实现音乐播放器,如果单单将播放音乐的器源操作放在子线程中进行会有一些问题:Thread所在的Activity优先级低,容易被系统杀死。音音乐通达智能源码如果程序在后台,乐播Thread所在的放源Activity很可能因为内存不足被系统杀死。(音乐播放器常常会在后台运行,战神抢单源码因为用户往往在听歌时会浏览其他应用。)
如果在播放音乐时需要更新Activity的UI界面,单单运用Thread会播出异常。poc源码怎么下载
参考思路:可以将播放音乐的操作放在Service中进行,如果需要更新UI,可以使用Hanlder与Activity建立通信。