2012-04-23 2 views
7

볼륨이 아닌 프로그램 볼륨을 변경하고 싶습니다. 윈 XP가 아닌 윈도우 7 (도 아마 비스타)에Win 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); 
} 

이 유일한 작품 : 지금은 다음과 같은 코드가 있습니다. Win 7에서 동일하게 동작 할 스크립트를 찾지 못했지만 마스터 볼륨을 변경하기 위해서만 사용합니다 (이후에는 그렇지 않습니다).

답변

6

코드가 정상적으로 작동했습니다 (몇 가지 조정할 수 있음).

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); 
} 

내가 응용 프로그램을 시작하고 슬라이더를 이동

다음 추가 볼륨 : 여기에 아주 간단한 WPF 테스트 응용 프로그램은 윈도우 7의 x64에서 실행되는 코드입니다 슬라이더와 동 기적으로 최소에서 최대로 이동하는 '볼륨 믹서'에 컨트롤이 나타납니다.

waveOutSetVolume의 반환 값을 검사해야합니다. 코드가 여전히 작동하지 않는다면 실마리를 줄 수 있습니다.

+0

이 코드를 사용하고 있습니다 : http://pastebin.com/RcRjfBu4 그러나 디버그 내게 '0'제공합니다. 볼륨도 변경되지 않습니다. – Devator

+0

Retval = 0은 성공을 의미하므로 운이 없음을 의미합니다. 내가 말했듯이, 그것은 나를 위해 일한다. Win7 SP1이 있다고 가정합니다. VS11 베타 버전도 설치되어 있습니다. – Phil

+0

그래, 나는 그것이 (절반 만) 작동하는지 확인할 수 있습니다. 슬라이더를 미끄러 뜨릴 때 볼륨이 변화하는 것을 볼 수 있습니다 (시각적으로 만 볼 수 있습니다) - http://dl.dropbox.com/u/6166898/slider.png를보십시오. 그러나 볼륨은 ** 변경되지 않았습니다 **! Windows 볼륨 슬라이더를 수동으로 슬라이드하면 ** 변경되지만 프로그램 내에서는 변경되지 않습니다. 내가 여기서 뭔가를 놓치고 있니? – Devator

1

오디오 세션 API 인 IAudioVolume 및 IAudioSessionNotification을 사용하면 현재 앱 볼륨을 수정하고 앱의 볼륨 슬라이더로 볼륨을 추적 할 수 있습니다.

당신은 Larry Osterman's blog article

사용하기 쉬운이 ISimpleVolume 인터페이스에서의 사용의 샘플 목록을 찾을 수 있습니다. Larry's blog에도 설명되어 있습니다.

관련 문제