Advertisement
Guest User

Untitled

a guest
Sep 5th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Windows;
  3. using NAudio;
  4. using NAudio.Wave;
  5.  
  6.  
  7. namespace CrystalAudioTests
  8. {
  9.     class AudioPlayback
  10.     {
  11.         public static IWavePlayer playbackDevice;
  12.         public static WaveStream fileStream;
  13.  
  14.         public static void PlayFile()
  15.         {
  16.             if (playbackDevice != null && fileStream != null && playbackDevice.PlaybackState != PlaybackState.Playing)
  17.             {
  18.                 playbackDevice.Play();
  19.             }
  20.         }
  21.  
  22.         public static void CreateDevice()
  23.         {
  24.             playbackDevice = new WaveOut { DesiredLatency = 200 };
  25.         }
  26.            
  27.         public static void OpenFile(string filename)
  28.         {
  29.             var inputStream = new AudioFileReader(filename);
  30.             fileStream = inputStream;
  31.             var aggregator = new SampleAggregator(inputStream);
  32.             playbackDevice.Init(aggregator);
  33.         }
  34.  
  35.         public static void Pause()
  36.         {
  37.            if (playbackDevice != null)
  38.            {
  39.                 playbackDevice.Pause();
  40.            }
  41.         }
  42.  
  43.         public static void Stop()
  44.         {
  45.            if (playbackDevice != null)
  46.             {
  47.                 playbackDevice.Stop();
  48.             }
  49.             if (fileStream != null)
  50.             {
  51.                 fileStream.Position = 0;
  52.             }
  53.        
  54.         }
  55.  
  56.         public static void CloseFile()
  57.         {
  58.             if (fileStream != null)
  59.             {
  60.                 fileStream.Dispose();
  61.                 fileStream = null;
  62.             }
  63.         }
  64.  
  65.         public static void EnsureDeviceCreated()
  66.         {
  67.             if (playbackDevice == null)
  68.             {
  69.                 CreateDevice();
  70.             }
  71.         }
  72.  
  73.         public static void Load(string fileName)
  74.         {
  75.            /*  Stop();
  76.             //CloseFile();
  77.             //EnsureDeviceCreated();
  78.             OpenFile(fileName);
  79.             * */
  80.             Stop();
  81.             CreateDevice();
  82.             OpenFile(fileName);
  83.             PlayFile();
  84.         }
  85.  
  86.         public static void Dispose()
  87.         {
  88.             Stop();
  89.             CloseFile();
  90.             if (playbackDevice != null)
  91.             {
  92.                 playbackDevice.Dispose();
  93.                 playbackDevice = null;
  94.             }
  95.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement