diff options
| author | william341 <[email protected]> | 2017-05-28 12:37:00 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-05-28 12:37:00 -0700 |
| commit | 771c20cfb3a703e0f1550fdcf9eb07b78298c944 (patch) | |
| tree | 59cb532e15ebff313fdba2be264d78ec0033f407 /ShiftOS.WinForms/Applications/AudioPlayer.cs | |
| parent | 496b0cbf8659c99203f48210fd39c572400ae623 (diff) | |
| parent | c7ba7d733c756d196f98dd4533289a1ef4db715f (diff) | |
| download | shiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.tar.gz shiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.tar.bz2 shiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.zip | |
Merge pull request #1 from shiftos-game/master
welp, no longer a dev.
Diffstat (limited to 'ShiftOS.WinForms/Applications/AudioPlayer.cs')
| -rw-r--r-- | ShiftOS.WinForms/Applications/AudioPlayer.cs | 181 |
1 files changed, 179 insertions, 2 deletions
diff --git a/ShiftOS.WinForms/Applications/AudioPlayer.cs b/ShiftOS.WinForms/Applications/AudioPlayer.cs index b8be6af..9940ddd 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,18 +48,24 @@ namespace ShiftOS.WinForms.Applications InitializeComponent(); } + NAudio.Wave.WaveOut o = null; + + public void OnLoad() { - + wpaudio.Hide(); } public void OnSkinLoad() { - + pgplaytime.Width = flcontrols.Width - btnplay.Width - 25; } public bool OnUnload() { + o?.Dispose(); + mp3?.Dispose(); + memstream?.Dispose(); return true; } @@ -61,5 +73,170 @@ 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; + this.Invoke(new Action(() => + { + pgplaytime.Value = (int)time; + })); + Thread.Sleep(50); + } + if (o.PlaybackState == NAudio.Wave.PlaybackState.Stopped) + { + this.Invoke(new Action(() => + { + if (lbtracks.SelectedIndex < lbtracks.Items.Count - 1) + { + lbtracks.SelectedIndex++; + } + else if (loopToolStripMenuItem.Checked == true) + { + lbtracks.SelectedIndex = 0; + } + })); + } + }).Start(); + } + + private void lbtracks_SelectedIndexChanged(object sender, EventArgs e) + { + try + { + Play(lbtracks.SelectedItem.ToString()); + } + catch { } + } + + bool scrubbing = false; + + private void startScrub(object sender, MouseEventArgs e) + { + scrubbing = true; + } + + static public double linear(double x, double x0, double x1, double y0, double y1) + { + if ((x1 - x0) == 0) + { + return (y0 + y1) / 2; + } + return y0 + (x - x0) * (y1 - y0) / (x1 - x0); + } + + private void pgplaytime_MouseMove(object sender, MouseEventArgs e) + { + if (mp3 != null) + try + { + if (scrubbing) + { + long s_pos = (long)linear(e.X, 0, pgplaytime.Width, 0, (double)mp3.Length); + mp3.Position = s_pos; + } + } + catch { } + } + + private void pgplaytime_MouseUp(object sender, MouseEventArgs e) + { + scrubbing = false; + } + } + + 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; + } + } } } |
