Adding Player Classes 78/260778/3
authoraman.jeph <aman.jeph@samsung.com>
Fri, 2 Jul 2021 11:16:18 +0000 (16:46 +0530)
committeraman.jeph <aman.jeph@samsung.com>
Tue, 6 Jul 2021 08:25:10 +0000 (13:55 +0530)
Change-Id: I3ef04a543739a19ae18c8d6d61fc6417e70cb4e4
Signed-off-by: aman.jeph <aman.jeph@samsung.com>
Core/MusicPlayer.cs [new file with mode: 0644]
Core/PlayerController.cs [new file with mode: 0644]

diff --git a/Core/MusicPlayer.cs b/Core/MusicPlayer.cs
new file mode 100644 (file)
index 0000000..30942d5
--- /dev/null
@@ -0,0 +1,180 @@
+using System;
+using System.Threading.Tasks;
+using Tizen.Multimedia;
+using MusicPlayer.Common;
+
+namespace MusicPlayer.Core
+{
+    public class MusicPlayer
+    {
+        private readonly Player player = new Player();
+
+        public MusicPlayer()
+        {
+            player.ErrorOccurred += OnErrorOccurred;
+            player.PlaybackCompleted += OnPlayBackCompleted;
+        }
+
+        public event EventHandler<PlayerEventArgs> PlayerEvent;
+
+        private enum PlayerValidationState
+        {
+            Start,
+            Stop,
+            Pause,
+            Resume,
+            Position,
+        }
+
+        public PlayerState State
+        {
+            get => player.State;
+        }
+
+        public void SetSource(string uri)
+        {
+            if(State != PlayerState.Idle)
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "Player is not in idle state, can't set source");
+                return;
+            }
+            try
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "setting uri: " + uri);
+                player.SetSource(new MediaUriSource(uri));
+            }
+            catch (UnauthorizedAccessException ex)
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "UnauthorizedAccessException: " + ex.Message);
+            }
+            catch (ObjectDisposedException ex)
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "ObjectDisposed: " + ex.Message);
+            }
+            catch (InvalidOperationException ex)
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "InvalidOperationException: " + ex.Message);
+            }
+        }
+
+        public async Task PreparePlayer()
+        {
+            try
+            {
+                await player.PrepareAsync();
+            }
+            catch (ObjectDisposedException ex)
+            {
+                Tizen.Log.Error("MusicPlayer", "ObjectDisposedException: "+ex.Message);
+            }
+            catch (InvalidOperationException ex)
+            {
+                Tizen.Log.Error("MusicPlayer", "InvalidOperationException: "+ex.Message);
+            }
+            finally // TODO do we really need this
+            {
+                //DisposePlayer();
+            }
+        }
+
+        public void Play()
+        {
+            if (ValidatePlayerState(PlayerValidationState.Start))
+            {
+                player.Start();
+            }
+            else
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "Invalid player state for playing: " + player.State.ToString());
+            }
+        }
+
+        public void Pause()
+        {
+            if(ValidatePlayerState(PlayerValidationState.Pause))
+            {
+                player.Pause();
+            }
+        }
+
+        public void Resume()
+        {
+            if (ValidatePlayerState(PlayerValidationState.Resume))
+            {
+                player.Start();
+            }
+        }
+
+        public void Stop()
+        {
+            if(ValidatePlayerState(PlayerValidationState.Stop))
+            {
+                player.Stop();
+            }
+        }
+
+        public int GetPosition()
+        {
+            if (ValidatePlayerState(PlayerValidationState.Position))
+            {
+                return player.GetPlayPosition();
+            }
+            else
+            {
+                return 0;
+            }
+        }
+
+        public void Unprepare()
+        {
+            player.Unprepare();
+        }
+
+        public void DisposePlayer()
+        {
+            player.Dispose();
+        }
+
+        public async Task SetPosition(int pos)
+        {
+            await player.SetPlayPositionAsync(pos, false);
+        }
+
+        private bool ValidatePlayerState(PlayerValidationState state)
+        {
+            if(state == PlayerValidationState.Start || state == PlayerValidationState.Position)
+            {
+                return (State == PlayerState.Ready || State == PlayerState.Playing || State == PlayerState.Paused);
+            }
+            else if(state == PlayerValidationState.Pause)
+            {
+                return (State == PlayerState.Playing);
+            }
+            else if(state == PlayerValidationState.Resume)
+            {
+                return (State == PlayerState.Paused);
+            }
+            else if(state == PlayerValidationState.Stop)
+            {
+                return (State == PlayerState.Paused || State == PlayerState.Playing);
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+        private void OnPlayBackCompleted(object sender, EventArgs e)
+        {
+            PlayerEvent?.Invoke(this, new PlayerEventArgs(EventType.PlaybackCompleted, "Playback Completed"));
+            Tizen.Log.Error(AppConstants.LogTag, "Playback completed");
+        }
+
+        // TODO this event is called on a differnt thread..need to handle it
+        private void OnErrorOccurred(object sender, PlayerErrorOccurredEventArgs e)
+        {
+            PlayerEvent.Invoke(this, new PlayerEventArgs(EventType.Error, e.Error.ToString()));
+            Tizen.Log.Error(AppConstants.LogTag, "Error Occurred: " + e.Error.ToString());
+        }
+    }
+}
diff --git a/Core/PlayerController.cs b/Core/PlayerController.cs
new file mode 100644 (file)
index 0000000..4cd4a80
--- /dev/null
@@ -0,0 +1,104 @@
+using System;
+using Tizen.Multimedia;
+using MusicPlayer.Common;
+
+namespace MusicPlayer.Core
+{
+    class PlayerController
+    {
+        private readonly MusicPlayer musicPlayer;
+        private static PlayerController instance = instance ??= new PlayerController();
+
+        private PlayerController()
+        {
+            musicPlayer = new MusicPlayer();
+            musicPlayer.PlayerEvent += OnPlayerEvent;
+        }
+
+        public event EventHandler<PlayerEventArgs> PlayerEventOccurred;
+
+        public static PlayerController Instance
+        {
+            get => instance;
+        }
+
+        private string uri;
+
+        public string Uri
+        {
+            get => uri;
+            set => uri = value;
+        }
+
+        public async void Play()
+        {
+            if(Uri == null)
+            {
+                Tizen.Log.Error(AppConstants.LogTag, "Please set the uri to play");
+                return;
+            }
+            Tizen.Log.Error(AppConstants.LogTag, "Current player state: "+musicPlayer.State.ToString());
+            if (musicPlayer.State == PlayerState.Paused || musicPlayer.State == PlayerState.Playing)
+            {
+                musicPlayer.Stop();
+            }
+            if(musicPlayer.State == PlayerState.Ready)
+            {
+                musicPlayer.Unprepare();
+            }
+            musicPlayer.SetSource(Uri);
+            if(musicPlayer.State == PlayerState.Idle)
+            {
+                await musicPlayer.PreparePlayer();
+            }
+            musicPlayer.Play();
+        }
+
+        public void Destroy()
+        {
+            musicPlayer?.DisposePlayer();
+        }
+
+        public void SetSource()
+        {
+            musicPlayer.SetSource(Uri);
+        }
+
+        public void Pause()
+        {
+            musicPlayer.Pause();
+        }
+
+        public void Resume()
+        {
+            if (musicPlayer.State == PlayerState.Paused)
+            {
+                musicPlayer.Resume();
+            }
+            else
+            {
+                Play();
+            }
+        }
+
+        public void Stop()
+        {
+            musicPlayer.Stop();
+        }
+
+        public int GetPosition()
+        {
+            return musicPlayer.GetPosition();
+        }
+
+        public async void SetPosition(int pos)
+        {
+            await musicPlayer.SetPosition(pos);
+        }
+
+        private void OnPlayerEvent(object sender, PlayerEventArgs e)
+        {
+            PlayerEventOccurred?.Invoke(this, e);
+        }
+    }
+}