gpt4 book ai didi

android - Android播放质量

转载 作者:行者123 更新时间:2023-12-03 00:09:58 25 4
gpt4 key购买 nike

我正在编写的应用程序的一部分要求我播放心音。当我使用下面的源代码进行播放时,会出现一些问题。

1)即使我将其设置为最大音量也不是很大声

2)听起来不对。听起来很多频率都被切断了

我从这个网站上听到了心声:http://depts.washington.edu/physdx/heart/demo.html

这是mp3类型的声音。
当我在计算机上播放时,即使将其设置为低音量,它的声音听起来也很完美。

Android不能播放某些频率范围吗?我正在使用Nexus 7

package com.example.heartsound;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HeartSound extends Activity {
MediaPlayer heartsound;
Button play;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_heart_sound);
//PLAY BUTTON
play = (Button) findViewById(R.id.playButton);
//Using MediaPlayer that will play Heart Sound
heartsound = MediaPlayer.create(HeartSound.this, R.raw.normal_heart_sound);
play.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (heartsound.isPlaying()) {
heartsound.stop();
heartsound.start();

} else {
heartsound.start();

}

}
});

}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
heartsound.stop();
finish();
}



}

最佳答案

我用ur 亲自测试了一个音频剪辑,即normal_heart_sounds 。当您将 4000hz频率频段设置为最大Eq级时,听起来非常不错。
因此,只需测试每个音频剪辑,并为每个音频剪辑设置均衡器

您还可以使用Equalizer.Settings备份设置并再次使用它们。
使用共享首选项。并保存每个的频段和均衡水平。当您开始播放乐队的声音设置 Eqalizer 时。

package com.example.demoact;

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.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {
private static final String TAG = "AudioFxDemo";

private static final float VISUALIZER_HEIGHT_DIP = 50f;

private MediaPlayer mMediaPlayer;
private Visualizer mVisualizer;
private Equalizer mEqualizer;

private LinearLayout mLinearLayout;
private VisualizerView mVisualizerView;
private TextView mStatusTextView;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

setVolumeControlStream(AudioManager.STREAM_MUSIC);

mStatusTextView = new TextView(this);
ScrollView scrollView= new ScrollView(this);
mLinearLayout = new LinearLayout(this);
scrollView.addView(mLinearLayout);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mLinearLayout.addView(mStatusTextView);

setContentView(scrollView);

// Create the MediaPlayer
mMediaPlayer = MediaPlayer.create(this, R.raw.normal);
Log.d(TAG, "MediaPlayer audio session ID: " + mMediaPlayer.getAudioSessionId());

setupVisualizerFxAndUI();
setupEqualizerFxAndUI();

// 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);

// When the stream ends, we don't need to collect any more data. We don't do this in
// setupVisualizerFxAndUI because we likely want to have more, non-Visualizer related code
// in this callback.
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mVisualizer.setEnabled(false);
mMediaPlayer.start();
}
});

mMediaPlayer.start();
mStatusTextView.setText("Playing audio...");
}

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.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
freqTextView.setGravity(Gravity.CENTER_HORIZONTAL);
freqTextView.setText((mEqualizer.getCenterFreq(band) / 1000) + " 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 / 100) + " dB");

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

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.weight = 1;
SeekBar bar = new SeekBar(this);
bar.setLayoutParams(layoutParams);
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(mEqualizer.getBandLevel(band));

bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mEqualizer.setBandLevel(band, (short) (progress + minEQLevel));
}

public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});

row.addView(minDbTextView);
row.addView(bar);
row.addView(maxDbTextView);

mLinearLayout.addView(row);
}
}

private void setupVisualizerFxAndUI() {
// Create a VisualizerView (defined below), which will render the simplified audio
// wave form to a Canvas.
mVisualizerView = new VisualizerView(this);
mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
(int)(VISUALIZER_HEIGHT_DIP * getResources().getDisplayMetrics().density)));
mLinearLayout.addView(mVisualizerView);

// Create the Visualizer object and attach it to our media player.
mVisualizer = new Visualizer(mMediaPlayer.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate) {
mVisualizerView.updateVisualizer(bytes);
}

public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
}, Visualizer.getMaxCaptureRate() / 2, true, false);
}

@Override
protected void onPause() {
super.onPause();

if (isFinishing() && mMediaPlayer != null) {
mVisualizer.release();
mEqualizer.release();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}

/**
* A simple class that draws waveform data received from a
* {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture }
*/
class VisualizerView extends View {
private byte[] mBytes;
private float[] mPoints;
private Rect mRect = new Rect();

private Paint mForePaint = new Paint();

public VisualizerView(Context context) {
super(context);
init();
}

private void init() {
mBytes = null;

mForePaint.setStrokeWidth(1f);
mForePaint.setAntiAlias(true);
mForePaint.setColor(Color.rgb(0, 128, 255));
}

public void updateVisualizer(byte[] bytes) {
mBytes = bytes;
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (mBytes == null) {
return;
}

if (mPoints == null || mPoints.length < mBytes.length * 4) {
mPoints = new float[mBytes.length * 4];
}

mRect.set(0, 0, getWidth(), getHeight());

for (int i = 0; i < mBytes.length - 1; i++) {
mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
mPoints[i * 4 + 1] = mRect.height() / 2
+ ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
mPoints[i * 4 + 3] = mRect.height() / 2
+ ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128;
}

canvas.drawLines(mPoints, mForePaint);
}
}

关于android - Android播放质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16259962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com