Bonjour,

je suis tout débutant en C# et je galère pour le contrôle du son.

Je tente de retranscrire un exemple winforms en WPF:

http://www.codeproject.com/Articles/...Volume-Control

J'ai bien réussi à implanter le "Timer", le slider, le contrôle, mais je n'arrive pas à récupérer le valeur du son en cours ce qui fait que mon appli lance le son à 100%.

Elle recup bien une valeur si je modifie le son dans windows, mais si je change dans l'appli, le prochain lancement réinitialise le tout.

Voila mon essai:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CoreAudioApi;
using System.Threading;
 
 
namespace Volume
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MMDevice device;
 
 
        public MainWindow()
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(timer1_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
 
            InitializeComponent();
 
            MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
            device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
            tbMaster.Value = (int)(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
            device.AudioEndpointVolume.OnVolumeNotification += new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);
 
        }
 
        private void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
        {
 
 
            if (this.Dispatcher.Thread == System.Threading.Thread.CurrentThread)
            {
                object[] Params = new object[1];
                Params[0] = data;
 
                this.Dispatcher.Invoke(new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification), Params);
 
 
 
            }
            else
            {
 
            }
        }
 
 
 
        private void tbMaster_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            device.AudioEndpointVolume.MasterVolumeLevelScalar = ((float)(tbMaster.Value / 100.0f));
 
        }
 
         private void timer1_Tick(object sender, EventArgs e)
        {
            VolumeMaster.Value = (int)(device.AudioMeterInformation.MasterPeakValue * 100);
 
        }
    }
}
Je ne comprend pas comment retranscrire cette partie qui lis le volume en cour:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
private void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
        {
            if (this.InvokeRequired)
            {
                object[] Params = new object[1];
                Params[0] = data;
                this.Invoke(new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification), Params);
            }
            else
            {
                tbMaster.Value = (int)(data.MasterVolume * 100);
            }
        }
J'ai découvert que les invoke n'était plus présent en WPF, je me suis tourné vers le dispatcher sans succès :s

Quelqu'un pourrait m’orienter m'expliquer voir résoudre le soucis ?

Merci