bonjour,
je travail sur une partie d'un projet ou je fais l'enregistrement audio avec silverlight ,
quand l'utilisateur clique sur recorde il y a un minuteur qui se lance aprés il clique sur stop et sur sauvegarder,
tout se passe bien,
ce que je veux c'est de fixé le minuteur pour qu'il s'arrete a 60 s par exemple ,
( j'arrive a le faire ) le souci c'est que meme si il s'arrete et il se fige a 60 s mais l''application continu a enregistré le son en attaendant de cliquer sur le bouton "stop" . et mois je veux synchronisé ça,
cad quand le minuteur ateint 60 s l'enregistrement du son s'arrete aussi .

voici mon code :

Code xml : 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
 
 
<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Codeplex_Dashboarding="clr-namespace:Codeplex.Dashboarding;assembly=Codeplex.Dashboarding" x:Class="proj.MainPage"
    mc:Ignorable="d" Background="#FF4F4545" Height="233" Width="641" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
 
    <Canvas x:Name="LayoutRoot" Background="#FF191818" Height="285" Width="786">
        <Button   Content="start" Command="{Binding RecordCommand}" x:Name="button1" Width="55" Height="39" Canvas.Left="76" Canvas.Top="95"/>
        <Button Content="Stop" Command="{Binding PlayPauseCommand}"  x:Name="button3" Width="55" Height="39" Canvas.Left="179" Canvas.Top="95"/>
        <Button Content="Save" Command="{Binding SaveCommand}" x:Name="button2" Width="55" Height="39" Canvas.Left="275" Canvas.Top="95"/>
        <TextBlock Text="{Binding StatusText}" x:Name="textBlock1" Foreground="#FF13E3BC" FontSize="12" Height="28" Canvas.Left="76" Canvas.Top="197" Width="557" />
        <TextBlock Height="34" Canvas.Left="415" TextWrapping="Wrap" Text="RECORDER" Canvas.Top="19" Width="200" Foreground="#FFFDFDFD" FontSize="26.667" FontFamily="Rockwell Extra Bold"/>
        <Rectangle Height="93" Canvas.Left="46" Stroke="#FFA39FA4" Canvas.Top="69" Width="552" StrokeThickness="5" RadiusX="10" RadiusY="10"/>
        <Codeplex_Dashboarding:Odometer x:Name="tapeMeter" Height="55" Canvas.Left="408" Canvas.Top="88" Width="164"  FinalValue="0020" InitialValue="0000" Interval="1" Digits="4" />
        <Path Data="M353.5,69 L354.5,162" Height="95" Canvas.Left="355.5" Stretch="Fill" Stroke="#FFA39FA4" StrokeThickness="5" Canvas.Top="67" UseLayoutRounding="False" Width="6"/>
        <sdk:Label Canvas.Left="12" Canvas.Top="248" Height="37" Name="label1" Width="646" Content="Merci de mettre votre nom sur le fichier" AllowDrop="False" FlowDirection="LeftToRight" FontSize="18" FontStretch="Normal" FontStyle="Italic" Foreground="#FFE22A10" HorizontalContentAlignment="Center">
            <sdk:Label.Background>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="Black" Offset="0" />
                    <GradientStop Color="#FFDB8181" Offset="1" />
                </LinearGradientBrush>
            </sdk:Label.Background>
            <sdk:Label.BorderBrush>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="Black" Offset="0" />
                    <GradientStop Color="#FFBE8686" Offset="1" />
                </LinearGradientBrush>
            </sdk:Label.BorderBrush>
        </sdk:Label>
    </Canvas>
</UserControl>


voici les class que j'utilise :

Code c# : 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
 
namespace proj.Audio
{
    public class WavManager
    {
        public static void SavePcmToWav(Stream rawData, Stream output, AudioFormat audioFormat)
        {
            if (audioFormat.WaveFormat != WaveFormatType.Pcm)
                throw new ArgumentException("Only PCM coding is supported.");
 
            BinaryWriter bwOutput = new BinaryWriter(output);
 
            // Write down the WAV header.
            // Refer to http://technology.niagarac.on.ca/courses/ctec1631/WavFileFormat.html
            // for details on the format.
 
            // Note that we use ToCharArray() when writing fixed strings
            // to force using the char[] overload because
            // Write(string) writes the string prefixed by its length.
 
            // -- RIFF chunk
 
            bwOutput.Write("RIFF".ToCharArray());
 
            // Total Length Of Package To Follow
            // Computed as data length plus the header length without the data
            // we have written so far and this data (44 - 4 ("RIFF") - 4 (this data))
            bwOutput.Write((uint)(rawData.Length + 36));
 
            bwOutput.Write("WAVE".ToCharArray());
 
            // -- FORMAT chunk
 
            bwOutput.Write("fmt ".ToCharArray());
 
            // Length Of FORMAT Chunk (Binary, always 0x10)
            bwOutput.Write((uint)0x10);
 
            // Always 0x01
            bwOutput.Write((ushort)0x01);
 
            // Channel Numbers (Always 0x01=Mono, 0x02=Stereo)
            bwOutput.Write((ushort)audioFormat.Channels);
 
            // Sample Rate (Binary, in Hz)
            bwOutput.Write((uint)audioFormat.SamplesPerSecond);
 
            // Bytes Per Second
            bwOutput.Write((uint)(audioFormat.BitsPerSample * audioFormat.SamplesPerSecond * audioFormat.Channels / 8));
 
            // Bytes Per Sample: 1=8 bit Mono, 2=8 bit Stereo or 16 bit Mono, 4=16 bit Stereo
            bwOutput.Write((ushort)(audioFormat.BitsPerSample * audioFormat.Channels / 8));
 
            // Bits Per Sample
            bwOutput.Write((ushort)audioFormat.BitsPerSample);
 
            // -- DATA chunk
 
            bwOutput.Write("data".ToCharArray());
 
            // Length Of Data To Follow
            bwOutput.Write((uint)rawData.Length);
 
            // Raw PCM data follows...
 
            // Reset position in rawData and remember its origin position
            // to restore at the end.
            long originalRawDataStreamPosition = rawData.Position;
            rawData.Seek(0, SeekOrigin.Begin);
 
            // Append all data from rawData stream into output stream.
            byte[] buffer = new byte[4096];
            int read;       // number of bytes read in one iteration
            while ((read = rawData.Read(buffer, 0, 4096)) > 0)
            {
                bwOutput.Write(buffer, 0, read);
            }
 
            rawData.Seek(originalRawDataStreamPosition, SeekOrigin.Begin);
        }
    }
}


Code c# : 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
 
 
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
 
namespace proj.Audio
{
    public class MemoryAudioSink : AudioSink
    {
        // Memory sink to record into.
        private MemoryStream _stream;
        // Current format the sinks records audio in.
        private AudioFormat _format;
 
        public Stream BackingStream
        {
            get { return _stream; }
        }
 
        public AudioFormat CurrentFormat
        {
            get { return _format; }
        }
 
        protected override void OnCaptureStarted()
        {
            _stream = new MemoryStream(1024);
        }
 
        protected override void OnCaptureStopped()
        {            
        }
 
        protected override void OnFormatChange(AudioFormat audioFormat)
        {
            if (audioFormat.WaveFormat != WaveFormatType.Pcm)
                throw new InvalidOperationException("MemoryAudioSink supports only PCM audio format.");
 
            _format = audioFormat;
        }
 
        protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
        {
            // New audio data arrived, write them to the stream.
            _stream.Write(sampleData, 0, sampleData.Length);
        }
    }
}

Code c# : 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
 
 
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using proj.Audio;
using System.IO;
using proj;
 
namespace proj
{
    public class SimpleCommand : ICommand
    {
        public Action ExecuteAction { get; set; }
 
        private bool _canExecute;
        public bool MayBeExecuted
        {
            get { return _canExecute; }
            set
            {
                if (_canExecute != value)
                {
                    _canExecute = value;
                    if (CanExecuteChanged != null)
                        CanExecuteChanged(this, new EventArgs());
                }
            }
        }
 
        #region ICommand Members
 
        public bool CanExecute(object parameter)
        {
            return MayBeExecuted;
        }
 
        public event EventHandler CanExecuteChanged;
 
        public void Execute(object parameter)
        {
            if (ExecuteAction != null)
                ExecuteAction();
        }
 
        #endregion
    }
 
    public class RecorderViewModel : DependencyObject
    {
        private SimpleCommand _recordCommand;
        private SimpleCommand _playPauseCommand;
        private SimpleCommand _saveCommand;
 
        private MemoryAudioSink _sink;
        private CaptureSource _captureSource;
        private SaveFileDialog saveFileDialog = new SaveFileDialog() { Filter = "Audio files (*.wav)|*.wav" };
 
        public SimpleCommand RecordCommand { get { return _recordCommand; } }
        public SimpleCommand PlayPauseCommand { get { return _playPauseCommand; } }
        public SimpleCommand SaveCommand { get { return _saveCommand; } }
 
        public static readonly DependencyProperty StatusTextProperty = DependencyProperty.Register("StatusText", typeof(string), typeof(RecorderViewModel), null);
        public string StatusText
        {
            get
            {
                return (string)GetValue(StatusTextProperty);
            }
            set
            {
                SetValue(StatusTextProperty, value);
            }
        }
 
        public RecorderViewModel()
        {
            _recordCommand = new SimpleCommand()
            {
                MayBeExecuted = true,
                ExecuteAction = () => Record()
            };
 
            _saveCommand = new SimpleCommand()
            {
                MayBeExecuted = false,
                ExecuteAction = () => SaveFile()
            };
 
            _playPauseCommand = new SimpleCommand()
            {
                MayBeExecuted = false,
                ExecuteAction = () => PlayOrPause()
            };
 
            var audioDevice = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice();
            _captureSource = new CaptureSource() { AudioCaptureDevice = audioDevice };
 
            GoToStartState();
        }
 
        private bool EnsureAudioAccess()
        {
            return CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess();
        }
 
        protected void GoToStartState()
        {
            StatusText = "prés...";
            _saveCommand.MayBeExecuted = false;
            _recordCommand.MayBeExecuted = true;
            _playPauseCommand.MayBeExecuted = false;
 
 
 
        }
 
        protected void Record()
        {
            if (!EnsureAudioAccess())
                return;
 
            if (_captureSource.State != CaptureState.Stopped)
                return;
 
            _sink = new MemoryAudioSink();
            _sink.CaptureSource = _captureSource;
            _captureSource.Start();
 
            // Enable pause command, disable record command
            _playPauseCommand.MayBeExecuted = true;
            _recordCommand.MayBeExecuted = false;
            StatusText = "enrgistrement encours...( merci de ne pas depassé 20 seconde, sinon ca sera pas pris en compte ";
        }
 
        public void PlayOrPause()
        {
            if (_captureSource.State == CaptureState.Started)
            {
                _captureSource.Stop();
 
                // Disable pause command, enable save command
                _playPauseCommand.MayBeExecuted = false;
                _saveCommand.MayBeExecuted = true;
                StatusText = "enrgesitrement terminer avec succé , merci de sauvegarder.";
            }
        }
 
        protected void SaveFile()
        {
            if (saveFileDialog.ShowDialog() == false)
            {
                return;
            }
 
            StatusText = "encours...";
 
 
            Stream stream = saveFileDialog.OpenFile();
 
            WavManager.SavePcmToWav(_sink.BackingStream, stream, _sink.CurrentFormat);
 
 
            stream.Close();
 
            MessageBox.Show("votre voix est bien enrgistrer sur votre poste, il vous reste qu'a l'Uploader avec le bouton parcourir sur l'autre page.");
 
 
 
 
 
            GoToStartState();
        }
    }
}


merci



Cdt