Advertisement
Ellesar

AudioSink problem

Apr 13th, 2011
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5.  
  6. public sealed class App : Application {
  7.     public App () {
  8.         this.Startup += this.Application_Startup;
  9.     }
  10.     TextBox log;
  11.     private void Application_Startup (object sender, StartupEventArgs e) {
  12.         StackPanel spMain = new StackPanel();
  13.         Button btnStart = new Button() {
  14.             Content = "Start"
  15.         };
  16.         btnStart.Click += new RoutedEventHandler((object s, RoutedEventArgs ev) => {
  17.             if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess()) {
  18.                 CaptureSource captureSource = new CaptureSource() {
  19.                     VideoCaptureDevice = null,
  20.                     AudioCaptureDevice = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice()
  21.                 };
  22.                 captureSource.CaptureFailed += new EventHandler<ExceptionRoutedEventArgs>((object se, ExceptionRoutedEventArgs eve) => {
  23.                     log.Dispatcher.BeginInvoke(new Action(() => {
  24.                         log.Text += "CaptureFailed\n";
  25.                     }));
  26.                 });
  27.                 SampleAudioSink audioSink = new SampleAudioSink(log) {
  28.                     CaptureSource = captureSource
  29.                 };
  30.                 captureSource.Start();
  31.             }
  32.         });
  33.         spMain.Children.Add(btnStart);
  34.         log = new TextBox() {
  35.             AcceptsReturn = true,
  36.             Height = 600,
  37.             IsReadOnly = true,
  38.             VerticalScrollBarVisibility = ScrollBarVisibility.Auto
  39.         };
  40.         spMain.Children.Add(log);
  41.         this.RootVisual = spMain;
  42.     }
  43. }
  44. internal sealed class SampleAudioSink : AudioSink {
  45.     TextBox log;
  46.     int i = 0;
  47.     internal SampleAudioSink (TextBox log) {
  48.         this.log = log;
  49.     }
  50.     protected override void OnCaptureStarted () {
  51.         log.Dispatcher.BeginInvoke(new Action(() => {
  52.             log.Text += "OnCaptureStarted\n";
  53.         }));
  54.     }
  55.     protected override void OnCaptureStopped () {
  56.         log.Dispatcher.BeginInvoke(new Action(() => {
  57.             log.Text += "OnCaptureStopped\n";
  58.         }));
  59.     }
  60.     protected override void OnFormatChange (AudioFormat audioFormat) {
  61.         log.Dispatcher.BeginInvoke(new Action(() => {
  62.             log.Text += "OnFormatChange\n";
  63.         }));
  64.     }
  65.     protected override void OnSamples (long sampleTimeInHundredNanoseconds, long sampleDurationInHundredNanoseconds, byte[] sampleData) {
  66.         log.Dispatcher.BeginInvoke(new Action(() => {
  67.             log.Text += string.Format("OnSamples ({0})\n", ++i);
  68.         }));
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement