fna-workbench

fna-workbench Git Source Tree


Root/src/Media/MediaPlayer.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
 * Copyright 2009-2016 Ethan Lee and the MonoGame Team
 *
 * Released under the Microsoft Public License.
 * See LICENSE for details.
 */
#endregion
 
#region Using Statements
using System;
#endregion
 
namespace Microsoft.Xna.Framework.Media
{
    public static class MediaPlayer
    {
        #region Public Static Properties
 
        public static bool GameHasControl
        {
            get
            {
                /* This is based on whether or not the player is playing custom
                 * music, rather than yours.
                 * -flibit
                 */
                return true;
            }
        }
 
        public static bool IsMuted
        {
            get
            {
                return INTERNAL_isMuted;
            }
 
            set
            {
                INTERNAL_isMuted = value;
 
                if (Queue.Count == 0)
                {
                    return;
                }
 
                Queue.SetVolume(value ? 0.0f : Volume);
            }
        }
 
        public static bool IsRepeating
        {
            get;
            set;
        }
 
        public static bool IsShuffled
        {
            get;
            set;
        }
 
        public static TimeSpan PlayPosition
        {
            get
            {
                if (Queue.ActiveSong == null)
                {
                    return TimeSpan.Zero;
                }
 
                return Queue.ActiveSong.Position;
            }
        }
 
        public static MediaQueue Queue
        {
            get;
            private set;
        }
 
        public static MediaState State
        {
            get
            {
                return INTERNAL_state;
            }
 
            private set
            {
                if (INTERNAL_state != value)
                {
                    INTERNAL_state = value;
                    if (MediaStateChanged != null)
                    {
                        MediaStateChanged(null, EventArgs.Empty);
                    }
                }
            }
        }
 
        public static float Volume
        {
            get
            {
                return INTERNAL_volume;
            }
            set
            {
                INTERNAL_volume = MathHelper.Clamp(value, 0.0f, 1.0f);
 
                if (Queue.ActiveSong == null)
                {
                    return;
                }
 
                Queue.SetVolume(IsMuted ? 0.0f : value);
            }
        }
 
        public static bool IsVisualizationEnabled
        {
            get;
            set;
        }
 
        #endregion
 
        #region Public Static Variables
 
        public static event EventHandler<EventArgs> ActiveSongChanged;
        public static event EventHandler<EventArgs> MediaStateChanged;
 
        #endregion
 
        #region Private Static Variables
 
        private static bool INTERNAL_isMuted = false;
        private static MediaState INTERNAL_state = MediaState.Stopped;
        private static float INTERNAL_volume = 1.0f;
 
        /* Need to hold onto this to keep track of how many songs
         * have played when in shuffle mode.
         */
        private static int numSongsInQueuePlayed = 0;
 
        #endregion
 
        #region Static Constructor
 
        static MediaPlayer()
        {
            Queue = new MediaQueue();
            IsVisualizationEnabled = false;
        }
 
        #endregion
 
        #region Public Static Methods
 
        public static void MoveNext()
        {
            NextSong(1);
        }
 
        public static void MovePrevious()
        {
            NextSong(-1);
        }
 
        public static void Pause()
        {
            if (State != MediaState.Playing || Queue.ActiveSong == null)
            {
                return;
            }
 
            Queue.ActiveSong.Pause();
 
            State = MediaState.Paused;
        }
 
        /// <summary>
        /// The Play method clears the current playback queue and queues the specified song
        /// for playback. Playback starts immediately at the beginning of the song.
        /// </summary>
        public static void Play(Song song)
        {
            Song previousSong = Queue.Count > 0 ? Queue[0] : null;
 
            Queue.Clear();
            numSongsInQueuePlayed = 0;
            Queue.Add(song);
            Queue.ActiveSongIndex = 0;
 
            PlaySong(song);
 
            if (previousSong != song && ActiveSongChanged != null)
            {
                ActiveSongChanged.Invoke(null, EventArgs.Empty);
            }
        }
 
        public static void Play(SongCollection songs)
        {
            Play(songs, 0);
        }
 
        public static void Play(SongCollection songs, int index)
        {
            Queue.Clear();
            numSongsInQueuePlayed = 0;
 
            foreach (Song song in songs)
            {
                Queue.Add(song);
            }
 
            Queue.ActiveSongIndex = index;
 
            PlaySong(Queue.ActiveSong);
        }
 
        public static void Resume()
        {
            if (State != MediaState.Paused)
            {
                return;
            }
 
            Queue.ActiveSong.Resume();
            State = MediaState.Playing;
        }
 
        public static void Stop()
        {
            if (State == MediaState.Stopped)
            {
                return;
            }
 
            // Loop through so that we reset the PlayCount as well.
            foreach (Song song in Queue.Songs)
            {
                Queue.ActiveSong.Stop();
            }
 
            State = MediaState.Stopped;
        }
 
        public static void GetVisualizationData(VisualizationData data)
        {
            if (IsVisualizationEnabled)
            {
                data.CalculateData(Queue.ActiveSong);
            }
        }
 
        #endregion
 
        #region Internal Static Methods
 
        internal static void OnSongFinishedPlaying(object sender, EventArgs args)
        {
            // TODO: Check args to see if song sucessfully played.
            numSongsInQueuePlayed += 1;
 
            if (numSongsInQueuePlayed >= Queue.Count)
            {
                numSongsInQueuePlayed = 0;
                if (!IsRepeating)
                {
                    Stop();
 
                    if (ActiveSongChanged != null)
                    {
                        ActiveSongChanged.Invoke(null, null);
                    }
 
                    return;
                }
            }
 
            MoveNext();
        }
 
        #endregion
 
        #region Private Static Methods
 
        private static void NextSong(int direction)
        {
            Stop();
            if (IsRepeating && Queue.ActiveSongIndex >= Queue.Count - 1)
            {
                Queue.ActiveSongIndex = 0;
 
                /* Setting direction to 0 will force the first song
                 * in the queue to be played.
                 * if we're on "shuffle", then it'll pick a random one
                 * anyway, regardless of the "direction".
                 */
                direction = 0;
            }
 
            Song nextSong = Queue.GetNextSong(direction, IsShuffled);
 
            if (nextSong != null)
            {
                PlaySong(nextSong);
            }
 
            if (ActiveSongChanged != null)
            {
                ActiveSongChanged.Invoke(null, null);
            }
        }
 
        private static void PlaySong(Song song)
        {
            song.Volume = IsMuted ? 0.0f : Volume;
            song.Play();
            State = MediaState.Playing;
        }
 
        #endregion
 
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.09252s using 11 queries.