Use NAudio to handle system sounds

This commit is contained in:
Michael 2017-04-15 14:39:24 -04:00
parent 761202b490
commit 51050a02d4
3 changed files with 42 additions and 48 deletions

View file

@ -27,6 +27,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
@ -44,15 +45,22 @@ namespace ShiftOS.WinForms.Applications
public Dialog()
{
InitializeComponent();
}
}
public string Title { get; private set; }
public void OnLoad()
{
AppearanceManager.SetWindowTitle(this, this.Title);
//NOT EVEn THIS WORKS
new Computer().Audio.Play(Properties.Resources.infobox, Microsoft.VisualBasic.AudioPlayMode.Background);
var str = Properties.Resources.infobox;
var bytes = new byte[str.Length];
str.Read(bytes, 0, bytes.Length);
ShiftOS.Engine.AudioManager.Stop();
if (File.Exists("snd.wav"))
File.Delete("snd.wav");
File.WriteAllBytes("snd.wav", bytes);
ShiftOS.Engine.AudioManager.Play("snd.wav");
}
public void OnSkinLoad()

View file

@ -37,11 +37,15 @@ namespace ShiftOS.WinForms.Applications
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
try
{
components.Dispose();
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
base.Dispose(disposing);
catch { }
}
#region Windows Form Designer generated code

View file

@ -42,51 +42,16 @@ namespace ShiftOS.Engine
private static IAudioProvider _provider = null;
private static bool _running = true;
public static void Stop()
{
_out?.Stop();
_reader?.Dispose();
_out?.Dispose();
}
public static void Init(IAudioProvider _p)
{
#if !NOSOUND
_provider = _p;
AppearanceManager.OnExit += () =>
{
_running = false;
_out?.Stop();
_reader?.Dispose();
_out?.Dispose();
System.IO.File.Delete("temp.mp3");
};
var t = new Thread(() =>
{
SaveSystem.GameReady += () =>
{
while(_out == null)
{
}
_out.Volume = _provider.Volume;
};
Random rnd = new Random();
while(_running == true)
{
int track = rnd.Next(0, _provider.Count);
byte[] mp3 = _provider.GetTrack(track);
System.IO.File.WriteAllBytes("temp.mp3", mp3);
_reader = new AudioFileReader("temp.mp3");
_out = new WaveOut();
_out.Init(_reader);
_out.Volume = _provider.Volume;
_out.Play();
while(_out.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(5000); //even when the player isn't playing, this will give a good delay between songs.
}
_reader.Dispose();
_out.Dispose();
}
});
t.IsBackground = true;
t.Start();
#endif
}
public static void SetVolume(float volume)
@ -95,6 +60,23 @@ namespace ShiftOS.Engine
_out.Volume = volume;
}
public static void Play(string file)
{
new Thread(() =>
{
try
{
_reader = new AudioFileReader(file);
_out = new WaveOut();
_out.Init(_reader);
_out.Volume = _provider.Volume;
_out.Play();
}
catch { }
}).Start();
}
internal static void Kill()
{
_running = false;