Advertisement
Guest User

MusicController.cs

a guest
Mar 2nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Media;
  3.  
  4. namespace AudioResumeTest
  5. {
  6.     public interface IMusicController
  7.     {
  8.         bool Enabled { get; set; }
  9.         bool IsPlaying { get; }
  10.         bool IsSuspended { get; }
  11.  
  12.         float Volume { get; set; }
  13.         bool IsMuted { get; set; }
  14.         bool IsRepeating { get; set; }
  15.  
  16.         void Play(Song song);
  17.         void Stop();
  18.         void Suspend();
  19.         void Resume();
  20.  
  21.         void Update(GameTime gameTime);
  22.     }
  23.  
  24.     public class MusicController : IMusicController
  25.     {
  26.         private Song _currentSong = null;
  27.         private TimeSpan _currentPosition = TimeSpan.Zero;
  28.         private TimeSpan _savedPosition = TimeSpan.Zero;
  29.         private IMusicPlayer _player = null;
  30.  
  31.         public MusicController(IMusicPlayer player)
  32.         {
  33.             _player = player ?? throw new ArgumentNullException();
  34.         }
  35.  
  36.         public bool IsPlaying { get; private set; } = false;
  37.         public bool IsSuspended { get; private set; } = false;
  38.  
  39.         public bool Enabled
  40.         {
  41.             get { return _enabled; }
  42.             set
  43.             {
  44.                 if (value != _enabled)
  45.                 {
  46.                     _enabled = value;
  47.  
  48.                     if (_enabled == false && this.IsPlaying)
  49.                     {
  50.                         var currentSong = _currentSong;
  51.                         this.Stop();
  52.                         _currentSong = currentSong;
  53.                     }
  54.                     else if (_enabled == true && _currentSong != null)
  55.                     {
  56.                         this.Play(_currentSong);
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.         private bool _enabled = true;
  62.  
  63.         public float Volume
  64.         {
  65.             get { return _player.Volume; }
  66.             set { _player.Volume = value; }
  67.         }
  68.  
  69.         public bool IsMuted
  70.         {
  71.             get { return _player.IsMuted; }
  72.             set { _player.IsMuted = value; }
  73.         }
  74.  
  75.         public bool IsRepeating
  76.         {
  77.             get { return _player.IsRepeating; }
  78.             set { _player.IsRepeating = value; }
  79.         }
  80.  
  81.         public void Play(Song s)
  82.         {
  83.             if (this.Enabled == false)
  84.             {
  85.                 _currentSong = s;
  86.                 return;
  87.             }
  88.  
  89.             _player.Play(s);
  90.  
  91.             this.IsPlaying = true;
  92.             _savedPosition = TimeSpan.Zero;
  93.             _currentSong = s;
  94.         }
  95.  
  96.         public void Suspend()
  97.         {
  98.             if (this.IsSuspended == false)
  99.             {
  100.                 _player.Stop();
  101.                 _savedPosition = _currentPosition;
  102.                 this.IsPlaying = false;
  103.                 this.IsSuspended = true;
  104.             }
  105.         }
  106.  
  107.         public void Resume()
  108.         {
  109.             if (this.IsSuspended == true)
  110.             {
  111.                 if (this.Enabled == true)
  112.                 {
  113.                     _player.Play(_currentSong, _savedPosition);
  114.                     _savedPosition = TimeSpan.Zero;
  115.                     this.IsPlaying = true;
  116.                 }
  117.  
  118.                 this.IsSuspended = false;
  119.             }
  120.         }
  121.  
  122.         public void Stop()
  123.         {
  124.             _player.Stop();
  125.             _savedPosition = TimeSpan.Zero;
  126.             this.IsPlaying = false;
  127.             _currentSong = null;
  128.         }
  129.  
  130.         public void Update(GameTime gameTime)
  131.         {
  132.             if (this.IsPlaying)
  133.                 _currentPosition = _player.PlayPosition;
  134.             else
  135.                 _currentPosition = TimeSpan.Zero;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement