Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Media;
- namespace AudioResumeTest
- {
- public interface IMusicController
- {
- bool Enabled { get; set; }
- bool IsPlaying { get; }
- bool IsSuspended { get; }
- float Volume { get; set; }
- bool IsMuted { get; set; }
- bool IsRepeating { get; set; }
- void Play(Song song);
- void Stop();
- void Suspend();
- void Resume();
- void Update(GameTime gameTime);
- }
- public class MusicController : IMusicController
- {
- private Song _currentSong = null;
- private TimeSpan _currentPosition = TimeSpan.Zero;
- private TimeSpan _savedPosition = TimeSpan.Zero;
- private IMusicPlayer _player = null;
- public MusicController(IMusicPlayer player)
- {
- _player = player ?? throw new ArgumentNullException();
- }
- public bool IsPlaying { get; private set; } = false;
- public bool IsSuspended { get; private set; } = false;
- public bool Enabled
- {
- get { return _enabled; }
- set
- {
- if (value != _enabled)
- {
- _enabled = value;
- if (_enabled == false && this.IsPlaying)
- {
- var currentSong = _currentSong;
- this.Stop();
- _currentSong = currentSong;
- }
- else if (_enabled == true && _currentSong != null)
- {
- this.Play(_currentSong);
- }
- }
- }
- }
- private bool _enabled = true;
- public float Volume
- {
- get { return _player.Volume; }
- set { _player.Volume = value; }
- }
- public bool IsMuted
- {
- get { return _player.IsMuted; }
- set { _player.IsMuted = value; }
- }
- public bool IsRepeating
- {
- get { return _player.IsRepeating; }
- set { _player.IsRepeating = value; }
- }
- public void Play(Song s)
- {
- if (this.Enabled == false)
- {
- _currentSong = s;
- return;
- }
- _player.Play(s);
- this.IsPlaying = true;
- _savedPosition = TimeSpan.Zero;
- _currentSong = s;
- }
- public void Suspend()
- {
- if (this.IsSuspended == false)
- {
- _player.Stop();
- _savedPosition = _currentPosition;
- this.IsPlaying = false;
- this.IsSuspended = true;
- }
- }
- public void Resume()
- {
- if (this.IsSuspended == true)
- {
- if (this.Enabled == true)
- {
- _player.Play(_currentSong, _savedPosition);
- _savedPosition = TimeSpan.Zero;
- this.IsPlaying = true;
- }
- this.IsSuspended = false;
- }
- }
- public void Stop()
- {
- _player.Stop();
- _savedPosition = TimeSpan.Zero;
- this.IsPlaying = false;
- _currentSong = null;
- }
- public void Update(GameTime gameTime)
- {
- if (this.IsPlaying)
- _currentPosition = _player.PlayPosition;
- else
- _currentPosition = TimeSpan.Zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement