关于params设置什么使播放器没有声音和完整的声音
谢谢
这个功能实际上很精彩。多亏了它,你可以用任意数量的步骤创建一个音量表!
我们假设您需要50个步骤:
int maxVolume = 50;
然后将setVolume设置为此范围内的任何值(0-49),执行以下操作:
float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume));
yourMediaPlayer.setVolume(log1,log1); //set volume takes two paramater
好,易于!并且不要使用AudioManager来设置音量!它会导致许多副作用,例如禁用静音模式,这会让你的用户发疯!
好吧,我做了以下代码,它稍微有效:
public class MainActivity extends Activity {
float volumeLevel = 0.5f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
volumeUp = findViewById(R.id.volUp);
volumeDown = findViewById(R.id.volDown);
song = MediaPlayer.create(this, R.raw.audioFile);
volumeUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
volumeLevel = volumeLevel + 0.1f;
song.setVolume(volumeLevel,volumeLevel);
}
});
volumeDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
volumeLevel = volumeLevel - 0.1f;
song.setVolume(volumeLevel,volumeLevel);
}
});
}
}
在user100858解决方案之后,我发布了我的确切代码:
private final static int MAX_VOLUME = 100;
...
...
final float volume = (float) (1 - (Math.log(MAX_VOLUME - soundVolume) / Math.log(MAX_VOLUME)));
mediaPlayer.setVolume(volume, volume);
soundVolume是您要设置的音量,介于0和MAX_VOLUME之间。所以在这个例子中0到100之间。
对于Android MediaPlayer.setVolume
,搜索网络似乎显示0.0f
没有声音,1.0f
完整的声音。
这里的其他答案不正确 - 或者至少,它们没有正确配置。
使用他们的代码(例如Tomasz或ssuukk的代码)执行以下测试:
1)将100设置为“最大音量”/步数,并提交音量50。
它返回:0.150514997831991
2)将1000设置为“最大音量”/步数,并提交音量500。
它返回了什么?相同的值,0.150514997831991,对吗?
不。相反,它是:0.100343331887994
换句话说,现有答案会根据您设置的音量步长来改变输入音量百分比(即变换曲线)的缩放方式。
我花了最后几个小时研究这个问题;足以让我不想详细解释这个问题。相反,我只会在我的程序中发布大型代码/注释块。 (它在C#中,对于Xamarin Android,但Java的功能应该相同)
public enum VolumeScaleType
{
//Energy, // what MediaPlayer possibly treats passed values as
Amplitude, // what MediaPlayer most likely treats passed values as
Loudness // what people treat everyday volume values as (as in "that sounded 2 times as loud")
}
// MediaPlayer
/*public static void SetVolume_IncorrectSOApproach(this MediaPlayer s, double volume, VolumeScaleType volumeType = VolumeScaleType.Loudness)
{
const int maxVolume = 100;
var volume_toScale = volume * maxVolume;
double volume_scalar = volumeType == VolumeScaleType.Amplitude ? volume : (1 - (Math.Log(maxVolume - volume_toScale) / Math.Log(maxVolume)));
s.SetVolume((float)volume_scalar, (float)volume_scalar);
}*/
public static void SetVolume_MyPossiblyCorrectApproach(this MediaPlayer s, double volume, VolumeScaleType volumeType = VolumeScaleType.Loudness)
{
// Links:
// 1) http://en.wikipedia.org/wiki/Decibel
// 2) http://trace.wisc.edu/docs/2004-About-dB
// 3) http://hyperphysics.phy-astr.gsu.edu/hbase/sound/loud.html
// 4) http://www.animations.physics.unsw.edu.au/jw/dB.htm
// 5) http://www.soundmaskingblog.com/2012/06/saved_by_the_bell
// 6) http://www.campanellaacoustics.com/faq.html
// 7) http://physics.stackexchange.com/questions/9113/how-sound-intensity-db-and-sound-pressure-level-db-are-related
// 8) http://www.sengpielaudio.com/calculator-loudness.htm (note: page uses terms 'power/intensity' and 'pressure' differently; power/intensity: for whole shell at distance, pressure: field-quantity?)
// basic idea: you can think of one decibel (of gain), + or -, as *translating into* the given changes-in/multipliers-for energy, amplitude, or loudness
// (i.e. one decibel provides a specific amount to multiply energy, amplitude, and loudness values, such that they remain aligned realistically)
// note: the 'one decibel' unit is set up to correspond roughly to a change in loudness just substantial enough to be noticeable
// note: the 'quietest perceivable sound' example (standard) base has these absolute values: 'e' is 1 pico-watt per square-foot, 'a' is 20 micropascals, 'l' is the quietest-perceivable-loudness
// references (for q.p.s. base) | db (gain) | energy | amplitude | loudness
// ===============================================================================================
// actual silence | -inf | 0 | 0 | 0
// (a seeming silence) | -20 | e / 100 | a / 10 | 0 (would be l / 4, if 'l' weren't already for the quietest-perceivable-sound)
// (a seeming silence) | -10 | e / 10 | a / 3.16227/sqrt(10) | 0 (would be l / 2, if 'l' weren't already for the quietest-perceivable-sound)
// quietest perceivable sound | 0 | e | a | l
// ? | 1 | e * 1.258925 | a * 1.122018 | l * 1.071773
// rustling leaves | 10 | e * 10 | a * 3.16227/sqrt(10) | l * 2
// whisper, or rural nighttime | 20 | e * 100 | a * 10 | l * 4
// watch ticking | 30 | e * 1000 | a * 31.622/sqrt(100) | l * 8
// quiet speech, or rural daytime | 40 | e * 10000 | a * 100 | l * 16
// dishwasher in next room | 50 | e * 100000 | a * 316/sqrt(100000) | l * 32
// ordinary conversation | 60 | e * 1000000 | a * 1000 | l * 64
// ===============================================================================================
// assuming MediaPlayer.SetVolume treats passed values as Amplitude
Func<double, double> convertLoudnessToAmplitude = loudness=>Math.Pow(10, Math.Log(loudness, 4));
var volume_amplitude = volumeType == VolumeScaleType.Amplitude ? volume : convertLoudnessToAmplitude(volume);
s.SetVolume((float)volume_amplitude, (float)volume_amplitude);
// assuming MediaPlayer.SetVolume treats passed values as Energy
//Func<double, double> convertLoudnessToEnergy = loudness=>Math.Pow(100, Math.Log(loudness, 4));
//var volume_energy = volumeType == VolumeScaleType.Energy ? volume : convertLoudnessToEnergy(volume);
//s.SetVolume((float)volume_energy, (float)volume_energy);
}
文档很稀疏,所以我无法确定我是否拥有SetVolume方法所期望的正确的缩放系统/单元类型。
假设它需要一个Amplitude值,上面的代码可能是正确的音量设置代码。 (获取所需的响度,线性,作为输入,并输出/设置内置SetVolume方法所需的Amplitude值)
不过,我不确定这是否正确,而且太累了,不能确认。如果有人有进一步的想法,请随意添加它们。 (3个多小时足以在一天内花在这样的问题上)
仔细聆听后,通过以下方式比较响度衰减效果:
我发现选项1似乎更接近线性响度淡入!换句话说......从实际收听和比较基本方法,到这里显示的各种转换方法,似乎文档是错误的,而SetVolume方法实际上只是期望线性标度上的响度值。 (也许他们已经更新了它,以便在最近的一个API版本中更直观地工作,但是没有更新文档?)
如果是这样,那肯定会让事情变得简单。这就是我现在要去的地方。 (虽然我会将指数/定标方法作为一个程序设置,我想,只是有借口保留所有时间投入的结果!)
如Venryx所述,建议的答案是错误的。日志数学不起作用(你必须减去,而不是分割日志以使它们按你想要的方式工作)。
无论如何,看起来Android音量设置现在与Loudness线性相称...所以0.5是大声为1.0的50%,0.1是10%等等。不需要复杂的日志数学来将分贝转换为响度。只需对大多数人进行线性设置即可。
我试过Android MediaPlayer.setVolume
,但这个功能没用。
我想我们应该使用下面的功能
AudioManager mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume * mLastProgress / 10, 0);
此代码将音量分解为10个相等的部分并增加或减少音量。
Button decreaseVolButton = (Button) findViewById(R.id.decrease_volumn);
Button increaseVolButton = (Button) findViewById(R.id.increase_volumn);
final MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.sample);
decreaseVolButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
volume = (float) (volume - 0.1);
mediaPlayer.setVolume(volume, volume);
}
});
increaseVolButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
volume = (float) (volume + 0.1);
mediaPlayer.setVolume(volume, volume);
}
});
如果要将音量设置为无声音,则传递(0f,0f)
如果要将音量设为全音,则传递(1f,1f)
我在这里看到的一切都达不到我的期望。我遇到的主要问题是,在0到50的范围内,25从不在中间,而是更接近最大声音。这里提出的日志功能对我来说几乎没有任何区别。
要阅读有关数学的更多信息,请参阅this answer。
变量
Linear input value = x // User-specified input value
Linear scale min,max = x1,x2 // My pre-determined range of 0-50 on my UI
Log scale min,max = y1,y2 // Normalizes the log result to between 0-1
Log value result = z // The output to pass to the setVolume() method
随着价值上涨而变化减速的公式(最简单的形式)
这种方法的问题在于,这与我们想要的android相反,因为它似乎已经默认执行此操作。当您传递线性值时,当值仍然很低时,它已经增加得太快,这进一步突出了这种效果。
x1 + (log(x) - log(x1)) / (log(x2) - log(x1)) * (y2 - y1) = z
随着价值上涨,变化加速的公式
这是适合我的方法;翻转输入以保持相同的变化率,但反转。有了这个,我可以在25左右获得一个完美的中音量,这是一个非常流畅的听觉体验,从0到50。
y2 - (x1 + (log(x2 + x1 - x) - log(x1)) / (log(x2) - log(x1)) * (y2 - y1)) = z
为什么要这么复杂?我正在使用这个简单的公式:
public float getVolume() {
float currVolume = (float) sp.getInt("volume", 10);
float maxVolume = 15.0f;
float result = currVolume / maxVolume;
return result;
}
并在媒体播放器中设置此值,如:
player.setVolume(getVolume(), getVolume());