Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows;
- using NAudio;
- using NAudio.Wave;
- namespace CrystalAudioTests
- {
- class AudioPlayback
- {
- public static IWavePlayer playbackDevice;
- public static WaveStream fileStream;
- public static void PlayFile()
- {
- if (playbackDevice != null && fileStream != null && playbackDevice.PlaybackState != PlaybackState.Playing)
- {
- playbackDevice.Play();
- }
- }
- public static void CreateDevice()
- {
- playbackDevice = new WaveOut { DesiredLatency = 200 };
- }
- public static void OpenFile(string filename)
- {
- var inputStream = new AudioFileReader(filename);
- fileStream = inputStream;
- var aggregator = new SampleAggregator(inputStream);
- playbackDevice.Init(aggregator);
- }
- public static void Pause()
- {
- if (playbackDevice != null)
- {
- playbackDevice.Pause();
- }
- }
- public static void Stop()
- {
- if (playbackDevice != null)
- {
- playbackDevice.Stop();
- }
- if (fileStream != null)
- {
- fileStream.Position = 0;
- }
- }
- public static void CloseFile()
- {
- if (fileStream != null)
- {
- fileStream.Dispose();
- fileStream = null;
- }
- }
- public static void EnsureDeviceCreated()
- {
- if (playbackDevice == null)
- {
- CreateDevice();
- }
- }
- public static void Load(string fileName)
- {
- /* Stop();
- //CloseFile();
- //EnsureDeviceCreated();
- OpenFile(fileName);
- * */
- Stop();
- CreateDevice();
- OpenFile(fileName);
- PlayFile();
- }
- public static void Dispose()
- {
- Stop();
- CloseFile();
- if (playbackDevice != null)
- {
- playbackDevice.Dispose();
- playbackDevice = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement