forked from naudio/NAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPageViewModel.cs
163 lines (137 loc) · 5.1 KB
/
MainPageViewModel.cs
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
using System;
using Windows.Storage.Streams;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml.Controls;
using System.IO;
using NAudio.MediaFoundation;
namespace NAudioUniversalDemo
{
class MainPageViewModel : ViewModelBase
{
private IWavePlayer player;
private WaveStream reader;
private IWaveIn recorder;
private MemoryStream recordStream;
private IRandomAccessStream selectedStream;
public MainPageViewModel()
{
LoadCommand = new DelegateCommand(Load);
PlayCommand = new DelegateCommand(Play) { IsEnabled = false };
PauseCommand = new DelegateCommand(Pause) { IsEnabled = false };
StopCommand = new DelegateCommand(Stop) { IsEnabled = false };
RecordCommand = new DelegateCommand(Record);
StopRecordingCommand = new DelegateCommand(StopRecording) { IsEnabled = false };
MediaFoundationApi.Startup();
}
private void Stop()
{
player?.Stop();
}
private void Pause()
{
player?.Pause();
}
private void Play()
{
if (player == null)
{
// Exclusive mode - fails with a weird buffer alignment error
player = new WasapiOutRT(AudioClientShareMode.Shared, 200);
player.Init(CreateReader);
player.PlaybackStopped += PlayerOnPlaybackStopped;
}
if (player.PlaybackState != PlaybackState.Playing)
{
//reader.Seek(0, SeekOrigin.Begin);
player.Play();
StopCommand.IsEnabled = true;
PauseCommand.IsEnabled = true;
LoadCommand.IsEnabled = false;
}
}
private IWaveProvider CreateReader()
{
if (reader is RawSourceWaveStream)
{
reader.Position = 0;
return reader;
}
reader = new MediaFoundationReaderUniversal(selectedStream);
return reader;
}
private void Record()
{
if (recorder == null)
{
recorder = new WasapiCaptureRT();
recorder.RecordingStopped += RecorderOnRecordingStopped;
recorder.DataAvailable += RecorderOnDataAvailable;
}
if (reader != null)
{
reader.Dispose();
reader = null;
}
recorder.StartRecording();
RecordCommand.IsEnabled = false;
StopRecordingCommand.IsEnabled = true;
}
private async void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
if (reader == null)
{
recordStream = new MemoryStream();
reader = new RawSourceWaveStream(recordStream, recorder.WaveFormat);
}
await recordStream.WriteAsync(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
}
private void StopRecording()
{
recorder?.StopRecording();
}
private void RecorderOnRecordingStopped(object sender, StoppedEventArgs stoppedEventArgs)
{
RecordCommand.IsEnabled = true;
StopRecordingCommand.IsEnabled = false;
PlayCommand.IsEnabled = true;
}
private void PlayerOnPlaybackStopped(object sender, StoppedEventArgs stoppedEventArgs)
{
LoadCommand.IsEnabled = true;
StopCommand.IsEnabled = false;
PauseCommand.IsEnabled = false;
if (reader != null)
{
reader.Position = 0;
}
}
private async void Load()
{
if (player != null)
{
player.Dispose();
player = null;
}
reader = null; // will be disposed by player
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
picker.FileTypeFilter.Add("*");
var file = await picker.PickSingleFileAsync();
if (file == null) return;
var stream = await file.OpenAsync(FileAccessMode.Read);
if (stream == null) return;
selectedStream = stream;
PlayCommand.IsEnabled = true;
}
public DelegateCommand LoadCommand { get; }
public DelegateCommand PlayCommand { get; }
public DelegateCommand PauseCommand { get; }
public DelegateCommand StopCommand { get; }
public DelegateCommand RecordCommand { get; }
public DelegateCommand StopRecordingCommand { get; }
public MediaElement MediaElement { get; set; }
}
}