aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/AudioPlayer.cs
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-04-24 21:01:42 -0400
committerMichael <[email protected]>2017-04-24 21:01:42 -0400
commitd4316e75fc876ec7ceac3f1a9362ef7b36ca40cf (patch)
tree94d937d8b695085f521d6a75991ef356cac0f59e /ShiftOS.WinForms/Applications/AudioPlayer.cs
parent7c070a8213fc59f289385d82b60c3a49c02e03a9 (diff)
downloadshiftos_thereturn-d4316e75fc876ec7ceac3f1a9362ef7b36ca40cf.tar.gz
shiftos_thereturn-d4316e75fc876ec7ceac3f1a9362ef7b36ca40cf.tar.bz2
shiftos_thereturn-d4316e75fc876ec7ceac3f1a9362ef7b36ca40cf.zip
Audio Player work.
Diffstat (limited to 'ShiftOS.WinForms/Applications/AudioPlayer.cs')
-rw-r--r--ShiftOS.WinForms/Applications/AudioPlayer.cs133
1 files changed, 132 insertions, 1 deletions
diff --git a/ShiftOS.WinForms/Applications/AudioPlayer.cs b/ShiftOS.WinForms/Applications/AudioPlayer.cs
index b8be6af..6d4d58a 100644
--- a/ShiftOS.WinForms/Applications/AudioPlayer.cs
+++ b/ShiftOS.WinForms/Applications/AudioPlayer.cs
@@ -32,9 +32,15 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShiftOS.Engine;
+using NAudio;
+using System.Threading;
namespace ShiftOS.WinForms.Applications
{
+ [AppscapeEntry("Audio Player", "Play music and other sounds on your computer.", 3047, 1000, "file_skimmer", "Entertainment")]
+ [Launcher("Audio Player", false, null, "Entertainment")]
+ [WinOpen("audio_player")]
+ [DefaultTitle("Audio Player")]
public partial class AudioPlayer : UserControl, IShiftOSWindow
{
public AudioPlayer()
@@ -42,9 +48,12 @@ namespace ShiftOS.WinForms.Applications
InitializeComponent();
}
+ NAudio.Wave.WaveOut o = null;
+
+
public void OnLoad()
{
-
+ wpaudio.Hide();
}
public void OnSkinLoad()
@@ -54,6 +63,9 @@ namespace ShiftOS.WinForms.Applications
public bool OnUnload()
{
+ o?.Dispose();
+ mp3?.Dispose();
+ memstream?.Dispose();
return true;
}
@@ -61,5 +73,124 @@ namespace ShiftOS.WinForms.Applications
{
}
+
+ private void addSongToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ FileSkimmerBackend.GetFile(new[] { ".mp3", ".wav" }, FileOpenerStyle.Open, (path) =>
+ {
+ if (!lbtracks.Items.Contains(path))
+ lbtracks.Items.Add(path);
+ else
+ Infobox.Show("Song already added!", "That song is already added to the Audio Player playlist.");
+ });
+ }
+
+ private void clearToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ lbtracks.Items.Clear();
+ }
+
+ private void shuffleToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ var lst = new object[lbtracks.Items.Count];
+ lbtracks.Items.CopyTo(lst, 0);
+ var shuffle = new List<object>(lst);
+ shuffle.Shuffle();
+ lbtracks.Items.Clear();
+ lbtracks.Items.AddRange(shuffle.ToArray());
+ }
+
+ private void removeToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ lbtracks.Items.RemoveAt(lbtracks.SelectedIndex);
+ }
+
+ private void btnplay_Click(object sender, EventArgs e)
+ {
+ if(o == null)
+ {
+ if (lbtracks.Items.Count > 0)
+ {
+ Play(lbtracks.Items[0].ToString());
+ btnplay.Text = "Pause";
+ }
+ else
+ Infobox.Show("Error", "No tracks to play! Please add a track to the playlist.");
+ }
+ else if(o.PlaybackState == NAudio.Wave.PlaybackState.Paused)
+ {
+ o.Resume();
+ btnplay.Text = "Pause";
+ }
+ else if(o.PlaybackState == NAudio.Wave.PlaybackState.Playing)
+ {
+ o.Pause();
+ btnplay.Text = "Play";
+ }
+ }
+
+ System.IO.Stream memstream = null;
+ NAudio.Wave.Mp3FileReader mp3 = null;
+
+ public void Play(string track)
+ {
+ if (o != null)
+ {
+ o.Dispose();
+ mp3.Dispose();
+ memstream.Dispose();
+ }
+ var bytes = ShiftOS.Objects.ShiftFS.Utils.ReadAllBytes(track);
+ memstream = new System.IO.MemoryStream(bytes);
+ mp3 = new NAudio.Wave.Mp3FileReader(memstream);
+ o = new NAudio.Wave.WaveOut();
+ o.Init(mp3);
+ o.Play();
+
+ pgplaytime.Value = 0;
+ pgplaytime.Maximum = (int)mp3.Length;
+ new Thread(() =>
+ {
+ while(o.PlaybackState == NAudio.Wave.PlaybackState.Playing || o.PlaybackState == NAudio.Wave.PlaybackState.Paused)
+ {
+ long time = mp3.Position;
+ if(time != mp3.Position)
+ {
+ time = mp3.Position;
+ this.Invoke(new Action(() =>
+ {
+ pgplaytime.Value = (int)time;
+ }));
+ }
+ }
+ }).Start();
+ }
+
+ private void lbtracks_SelectedIndexChanged(object sender, EventArgs e)
+ {
+ try
+ {
+ Play(lbtracks.SelectedItem.ToString());
+ }
+ catch { }
+ }
+ }
+
+ public static class ListExtensions
+ {
+ private static Random rng = new Random();
+
+ public static void Shuffle<T>(this IList<T> list)
+ {
+ int n = list.Count;
+ while (n > 1)
+ {
+ n--;
+ int k = rng.Next(n + 1);
+ T value = list[k];
+ list[k] = list[n];
+ list[n] = value;
+ }
+ }
}
}