7

プログラムのボリューム (マスターボリュームではなく) を変更したい。現在、次のコードがあります。

DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);

private void volumeBar_Scroll(object sender, EventArgs e)
{
    // Calculate the volume that's being set
    int NewVolume = ((ushort.MaxValue / 10) * volumeBar.Value);
    // Set the same volume for both the left and the right channels
    uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
    // Set the volume
    waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
}

これは Windows XP でのみ機能し、Windows 7 では機能しません (おそらく Vista でも機能しません)。マスターボリュームを変更するためだけに、Win 7で同じことを達成するスクリプトは見つかりませんでした(私は後でありません)。

4

2 に答える 2

6

あなたのコードは私にとってはうまくいきました(いくつかの微調整で)。Windows 7 x64 で実行される非常に単純な WPF テスト アプリのコードを次に示します。

Xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Slider Minimum="0" Maximum="10" ValueChanged="ValueChanged"/>
    </Grid>
</Window>

C#

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        // Calculate the volume that's being set
        double newVolume = ushort.MaxValue * e.NewValue / 10.0;

        uint v = ((uint) newVolume) & 0xffff;
        uint vAll = v | (v << 16);

        // Set the volume
        int retVal = NativeMethods.WaveOutSetVolume(IntPtr.Zero, vAll);

        Debug.WriteLine(retVal);

        bool playRetVal = NativeMethods.PlaySound("tada.wav", IntPtr.Zero, 0x2001);

        Debug.WriteLine(playRetVal);
    }
}

static class NativeMethods
{
    [DllImport("winmm.dll", EntryPoint = "waveOutSetVolume")]
    public static extern int WaveOutSetVolume(IntPtr hwo, uint dwVolume);

    [DllImport("winmm.dll", SetLastError = true)]
    public static extern bool PlaySound(string pszSound, IntPtr hmod, uint fdwSound);
}

アプリを起動してスライダーを動かすと、スライダーと同期して最小から最大まで移動する「ボリューム ミキサー」に追加のボリューム コントロールが表示されます。

waveOutSetVolume からの戻り値を調べる必要があります。コードがまだ機能しない場合は、手がかりが得られる場合があります。

于 2012-04-25T18:54:13.313 に答える
1

オーディオセッションAPIのIAudioVolumeとIAudioSessionNotificationを使用して、現在のアプリの音量を変更したり、アプリの音量スライダーで音量を追跡したりできます。

それらの使用例のリストは、LarryOstermanのブログ記事にあります。

最も使いやすいのはISimpleVolumeインターフェースです。それはラリーのブログでも議論されています。

于 2012-04-25T17:16:14.473 に答える