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
|
class RadioStream
{
public RadioStream(string streamingUrl, string fullFilename)
{
webRequest = WebRequest.Create(streamingUrl);
this.FullFilename = fullFilename;
}
public string FullFilename { get; set; }
WebRequest webRequest;
WebResponse webResponse;
BufferedStream bufferedStream;
public bool IsRecording;
private bool stopped = true;
public delegate void RecordingStartedEventhandler();
public event RecordingStartedEventhandler RecordingStarted;
public delegate void RecordingStoppedEventhandler();
public event RecordingStoppedEventhandler RecordingStopped;
public delegate void RecordingProgressChangedEventhandler(object sender, RecordingEventArgs args);
public event RecordingProgressChangedEventhandler RecordingProgressChanged;
public void StartRecording()
{
stopped = false;
IsRecording = true;
webResponse = webRequest.GetResponse();
bufferedStream = new BufferedStream(webResponse.GetResponseStream());
using (FileStream outputStream = new FileStream(this.FullFilename, FileMode.Create, FileAccess.Write))
{
int cnt = 0;
const int LEN = 9;
byte[] buffer = new byte[LEN];
while ((cnt = bufferedStream.Read(buffer, 0, LEN)) != 0)
{
if (stopped == true)
{
break;
}
outputStream.Write(buffer, 0, cnt);
RecordingProgressChanged(this, new RecordingEventArgs(outputStream.Length));
}
}
if (RecordingStarted != null)
{
RecordingStarted();
}
}
public void StopRecording()
{
stopped = true;
IsRecording = false;
if (RecordingStopped != null)
{
RecordingStopped();
}
}
}
public class RecordingEventArgs : EventArgs
{
public long StreamLength { get; set; }
public RecordingEventArgs(long streamLength)
{
this.StreamLength = streamLength;
}
}
|