Android:MediaPlayer setVolume功能

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来设置音量!它会导致许多副作用,例如禁用静音模式,这会让你的用户发疯!

  • +1000我一直在寻找这个解决方案.太棒了.谢谢! (7认同)
  • @Vadim `currVolume` 是 [0,maxVolume-1] 范围内的整数,因此它不可能具有 `maxVolume` 的值。请注意,对于 `currVolume == maxVolume-1` 你会得到 `log1 == 0` 和 `setVolume(1)`(或者,使用该方法的当前签名:`setVolume(1,1)`),这意味着完整的音量. (2认同)

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之间.

  • 什么是`soundVolume`?它起源于哪里? (6认同)
  • @SomeoneSomewhere ......它来自SomeWhere ..:P (6认同)
  • 对于那些你想知道的人来说,soundVolume是你想要卷的0-100之间的变量.如果您想要更精细的调整曲调,请考虑将maxVolume设置为1000,然后将声音音量保持在0-1000之间. (3认同)
  • 老兄,这是我们想要设定的音量......:P (2认同)

typ*_*.pl 42

因为Android MediaPlayer.setVolume,搜索网络似乎显示0.0f没有声音,1.0f完整的声音.

  • 这不是正确的解决方案.它不会按比例缩放音量.我的意思是音量在0到0.5之间快速增长,但在0.5到1.0之间非常缓慢.检查下面的user100858解决方案,效果很好. (12认同)
  • 这个答案已经过时了。在较新版本的 Android 中,“0.5”的响度是“1”的 50%,这很容易实现。(文档确实在这方面似乎已经过时,但您可以通过仔细的耳听测试/比较来确认这一点 - 无论如何,这才是最终重要的) (2认同)

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方法提交所需的响度即可.
  2. 在发送之前(基本上)指望期望响度,使其成为SetVolume方法所期望的幅度(或类似)值.

我发现选项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)

  • 什么使`MediaPlayer.setVolume`无用?你不能做什么? (8认同)
  • AudioManager将设置您不想要的系统音量.如果用户的手机处于静音状态,并且您突然爆出音乐,他们将不会欣赏它;) (3认同)