031 DirectShow的音量控制

Post date: 2015/4/23 上午 07:29:11

本来这个问题没有任何悬念,但是,事实上并不是简单调用一下IBasicAudio.put_Volume就成了。我的实现代码如下,已在调试中通过,多谢VC+DirectShow+AVS的“上海--阿易”兄的帮助。

private int[] volumes = new int[]{-10000,-6418,-6147,-6000,

-5892,-4826,-4647,-4540

-4477, -4162,-3876, -3614, -3500,

-3492,-3374,-3261,-3100,-3153,-3048,-2947,-2849,-2755,-2700,

-2663,-2575,-2520,-2489,-2406,-2325,-2280,-2246,-2170,-2095,-2050,

-2023,-1952,-1900, -1884,-1834, -1820, -1800,-1780, -1757,-1695,-1636,-1579,

-1521,-1500,-1464,-1436,-1420, -1408,-1353,-1299,-1246,-1195,-1144,

-1096,-1060, -1049,-1020,-1003,-957,-912,-868, -800, -774,-784, -760, -744,

-705,-667,-630,-610,-594,-570 ,-558,-525,-493,-462,-432,-403,

-375,-348,-322,-297,-285, -273,-250,-228,-207,-187,-176, -168,

-150,-102,-75,-19,-10,0,0};

/// <summary>

/// 获得、设置音量

/// </summary>

public int Volume

{

get

{

if (basicAudio == null) return 0;

int hr = 0, volume = 0;

hr = basicAudio.get_Volume(out volume);

DsError.ThrowExceptionForHR(hr);

foreach (int v in volumes)

if (v >= volume) { volume = v; break; }

return volume;

}

set

{

if (basicAudio == null) return;

if (value < 0) value = 0;

if (value >= 100) value = 99;

int hr = 0;

hr = basicAudio.put_Volume(volumes[value]);

DsError.ThrowExceptionForHR(hr);

}

}

本来,directshow中的音量范围是在-10000至0之间,但是我发现,0总是代表当前已有的音量,也就是说播放器只能在已有音量上减小,而不能有所增加。这是个很让人头痛的问题。阿易兄的vc版实现启发了我。