Qin*_*ing 55 audio android volume media-player
关于params设置什么使播放器没有声音和完整的声音
谢谢
ssu*_*ukk 115
这个功能非常精彩.多亏了它,你可以用任意数量的步骤创建一个音量表!
我们假设您需要50个步骤:
int maxVolume = 50;
Run Code Online (Sandbox Code Playgroud)
然后将setVolume设置为此范围内的任何值(0-49),执行以下操作:
float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume));
yourMediaPlayer.setVolume(log1,log1); //set volume takes two paramater
Run Code Online (Sandbox Code Playgroud)
好,易于!并且不要使用AudioManager来设置音量!它会导致许多副作用,例如禁用静音模式,这会让你的用户发疯!
Tom*_*asz 51
在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);
Run Code Online (Sandbox Code Playgroud)
soundVolume是您要设置的音量,介于0和MAX_VOLUME之间.所以在这个例子中0到100之间.
typ*_*.pl 42
因为Android MediaPlayer.setVolume,搜索网络似乎显示0.0f没有声音,1.0f完整的声音.
Ven*_*ryx 13
这里的其他答案不正确 - 或者至少,它们没有正确配置.
使用他们的代码(例如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);
}
Run Code Online (Sandbox Code Playgroud)
文档很稀疏,所以我无法确定我是否拥有SetVolume方法所期望的正确的缩放系统/单元类型.
假设它需要一个Amplitude值,上面的代码可能是正确的音量设置代码.(获取所需的响度,线性,作为输入,并输出/设置内置SetVolume方法所需的Amplitude值)
不过,我不确定这是否正确,而且太累了,不能确认.如果有人有进一步的想法,请随意添加它们.(3个多小时足以在一天内花在这样的问题上)
仔细聆听后,通过以下方式比较响度衰减效果:
我发现选项1似乎更接近线性响度淡入!换句话说......从实际收听和比较基本方法,到这里显示的各种转换方法,似乎文档是错误的,而SetVolume方法实际上只是期望线性标度上的响度值.(也许他们已经更新了它,以便在最近的一个API版本中更直观地工作,但是没有更新文档?)
如果是这样,那肯定会让事情变得简单.这就是我现在要去的地方.(虽然我会将指数/尺度固定方法作为一个程序设置,我想,只是有借口保留所有时间投入的结果!)
小智 9
如Venryx所述,建议的答案是错误的.日志数学不起作用(你必须减去,而不是分割日志以使它们按你想要的方式工作).
无论如何,看起来Android音量设置现在与Loudness线性相称...所以0.5是大声为1.0的50%,0.1是10%等等.不需要复杂的日志数学来将分贝转换为响度.只需对大多数人进行线性设置即可.
小智 5
我试过Android MediaPlayer.setVolume,但这个功能没用.
我想我们应该使用下面的功能
AudioManager mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume * mLastProgress / 10, 0);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85156 次 |
| 最近记录: |