-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadioEntry.cs
189 lines (158 loc) · 4.27 KB
/
RadioEntry.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright(C) 2017 Michal Heczko
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
*/
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Un4seen.Bass;
namespace InfovojnaRadio
{
public class RadioEntry
{
#region Fields
private bool _active;
private double _balance;
private ShoutcastMetadata _previouseInfo;
private bool _mute;
private string _name;
private double _volume;
#endregion
#region Properties
internal double Balance
{
get { return _balance; }
set
{
_balance = value;
SetBalance(_balance);
}
}
internal int ChannelHandle { get; set; }
internal ShoutcastMetadata Info { get { return ShoutcastMetadata.From(Marshal.PtrToStringAnsi(Bass.BASS_ChannelGetTags(ChannelHandle, BASSTag.BASS_TAG_META))); } }
internal bool IsActive
{
get { return _active; }
private set { _active = MenuItem.DefaultItem = value; }
}
internal bool IsNewSong
{
get
{
ShoutcastMetadata info = Info;
if ((_previouseInfo == null && info == null) || (_previouseInfo != null && info == null))
return false;
if (_previouseInfo == null && info != null)
{
_previouseInfo = info;
return true;
}
if (_previouseInfo.Title.Equals(info.Title))
return false;
_previouseInfo = info;
return true;
}
}
internal MenuItem MenuItem { get; }
internal bool Mute
{
get { return _mute; }
set
{
_mute = value;
SetVolume(_mute ? 0 : Properties.Settings.Default.Volume);
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
MenuItem.Text = _name;
}
}
internal BASSActive Status
{
get
{
BASSActive status = ChannelHandle != 0 ? Bass.BASS_ChannelIsActive(ChannelHandle) : BASSActive.BASS_ACTIVE_STOPPED; ;
IsActive = status != BASSActive.BASS_ACTIVE_STOPPED;
return status;
}
}
internal double Volume
{
get { return _volume; }
set
{
_volume = value;
if (!_mute)
SetVolume(_volume);
}
}
public string Url { get; set; }
#endregion
#region Methods
public void Play()
{
OnBeforePlay?.Invoke(this, EventArgs.Empty);
ChannelHandle = Bass.BASS_StreamCreateURL(Url, 0, BASSFlag.BASS_DEFAULT, null, IntPtr.Zero);
if (ChannelHandle != 0)
{
if (!(IsActive = Bass.BASS_ChannelPlay(ChannelHandle, false)))
App.Instance.ShowBallonTip(string.Format("BASS_ChannelPlay: {0}", Bass.BASS_ErrorGetCode().ToString()));
}
else
App.Instance.ShowBallonTip(string.Format("BASS_StreamCreateURL: {0}", Bass.BASS_ErrorGetCode().ToString()));
OnAfterPlay?.Invoke(this, EventArgs.Empty);
}
protected void SetBalance(double balance)
{
if (ChannelHandle != 0)
if (!Bass.BASS_ChannelSetAttribute(ChannelHandle, BASSAttribute.BASS_ATTRIB_PAN, (float)balance / 100))
App.Instance.ShowBallonTip(string.Format("BASS_ChannelSetAttribute: {0}", Bass.BASS_ErrorGetCode().ToString()));
}
protected void SetVolume(double volume)
{
if (ChannelHandle != 0)
if (!Bass.BASS_ChannelSetAttribute(ChannelHandle, BASSAttribute.BASS_ATTRIB_VOL, (float)volume / 100))
App.Instance.ShowBallonTip(string.Format("BASS_ChannelSetAttribute: {0}", Bass.BASS_ErrorGetCode().ToString()));
}
public void Stop()
{
OnBeforeStop?.Invoke(this, EventArgs.Empty);
if (Bass.BASS_ChannelStop(ChannelHandle))
{
Bass.BASS_StreamFree(ChannelHandle);
ChannelHandle = 0;
IsActive = false;
}
OnAfterStop?.Invoke(this, EventArgs.Empty);
}
#endregion
#region Constructor(s)
public RadioEntry()
{
ChannelHandle = 0;
MenuItem = new MenuItem();
MenuItem.Click += (object sender, EventArgs args) => { Play(); };
}
public RadioEntry(string name, string url) : this()
{
MenuItem.Text = name;
Name = name;
Url = url;
}
#endregion
#region Events
public event EventHandler OnAfterPlay;
public event EventHandler OnAfterStop;
public event EventHandler OnBeforePlay;
public event EventHandler OnBeforeStop;
#endregion
}
}