Cleanup of sound code, also added the possibility to configure your own sound.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1911 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-06-08 11:43:05 +00:00
parent 5d742ce21d
commit b3a3c1da4e
3 changed files with 50 additions and 31 deletions

View file

@ -24,6 +24,9 @@ using System.Resources;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using GreenshotPlugin.UnmanagedHelpers; using GreenshotPlugin.UnmanagedHelpers;
using GreenshotPlugin.Core;
using Greenshot.IniFile;
using System.IO;
/// <summary> /// <summary>
/// Create to fix the sometimes wrongly played sample, especially after first start from IDE /// Create to fix the sometimes wrongly played sample, especially after first start from IDE
@ -35,33 +38,45 @@ namespace Greenshot.Helpers {
/// </summary> /// </summary>
public static class SoundHelper { public static class SoundHelper {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(SoundHelper)); private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(SoundHelper));
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static GCHandle? gcHandle = null; private static GCHandle? gcHandle = null;
private static byte[] soundBuffer = null; private static byte[] soundBuffer = null;
public static void Initialize() { public static void Initialize() {
try { try {
ResourceManager resources = new ResourceManager("Greenshot.Sounds", Assembly.GetExecutingAssembly()); ResourceManager resources = new ResourceManager("Greenshot.Sounds", Assembly.GetExecutingAssembly());
soundBuffer = (byte[])resources.GetObject("camera"); soundBuffer = (byte[])resources.GetObject("camera");
// Pin sound so it can't be moved by the Garbage Collector, this was the cause for the bad sound
gcHandle = GCHandle.Alloc(soundBuffer, GCHandleType.Pinned); if (conf.NotificationSound != null && conf.NotificationSound.EndsWith(".wav")) {
try {
if (File.Exists(conf.NotificationSound)) {
soundBuffer = File.ReadAllBytes(conf.NotificationSound);
}
} catch (Exception ex) {
LOG.WarnFormat("couldn't load {0}: {1}", conf.NotificationSound, ex.Message);
}
}
// Pin sound so it can't be moved by the Garbage Collector, this was the cause for the bad sound
gcHandle = GCHandle.Alloc(soundBuffer, GCHandleType.Pinned);
} catch (Exception e) { } catch (Exception e) {
LOG.Error("Error initializing.", e); LOG.Error("Error initializing.", e);
} }
} }
public static void Play() { public static void Play() {
SoundFlags flags = SoundFlags.SND_ASYNC | SoundFlags.SND_MEMORY; if (soundBuffer != null) {
//Thread playSoundThread = new Thread(delegate() {
try { SoundFlags flags = SoundFlags.SND_ASYNC | SoundFlags.SND_MEMORY | SoundFlags.SND_NOWAIT | SoundFlags.SND_NOSTOP;
if (soundBuffer != null) { try {
WinMM.PlaySound(gcHandle.Value.AddrOfPinnedObject(), (UIntPtr)0, (uint)flags); WinMM.PlaySound(gcHandle.Value.AddrOfPinnedObject(), (UIntPtr)0, (uint)flags);
} else { } catch (Exception e) {
WinMM.PlaySound((byte[])null, (UIntPtr)0, (uint)flags); LOG.Error("Error in play.", e);
} }
} catch (Exception e) { //});
LOG.Error("Error in play.", e); //playSoundThread.Name = "Play camera sound";
} //playSoundThread.IsBackground = true;
//playSoundThread.Start();
}
} }
public static void Deinitialize() { public static void Deinitialize() {
@ -75,19 +90,5 @@ namespace Greenshot.Helpers {
LOG.Error("Error in deinitialize.", e); LOG.Error("Error in deinitialize.", e);
} }
} }
[Flags]
public enum SoundFlags : int {
SND_SYNC = 0x0000, // play synchronously (default)
SND_ASYNC = 0x0001, // play asynchronously
SND_NODEFAULT = 0x0002, // silence (!default) if sound not found
SND_MEMORY = 0x0004, // pszSound points to a memory file
SND_LOOP = 0x0008, // loop the sound until next sndPlaySound
SND_NOSTOP = 0x0010, // don't stop any currently playing sound
SND_NOWAIT = 0x00002000, // don't wait if the driver is busy
SND_ALIAS = 0x00010000, // name is a registry alias
SND_ALIAS_ID = 0x00110000, // alias is a predefined id
SND_FILENAME = 0x00020000, // name is file name
}
} }
} }

View file

@ -134,7 +134,8 @@ namespace GreenshotPlugin.Core {
public bool OutputPrintFooter; public bool OutputPrintFooter;
[IniProperty("OutputPrintFooterPattern", Description = "Footer pattern", DefaultValue = "${capturetime:d\"D\"} ${capturetime:d\"T\"} - ${title}")] [IniProperty("OutputPrintFooterPattern", Description = "Footer pattern", DefaultValue = "${capturetime:d\"D\"} ${capturetime:d\"T\"} - ${title}")]
public string OutputPrintFooterPattern; public string OutputPrintFooterPattern;
[IniProperty("NotificationSound", Description = "The wav-file to play when a capture is taken, loaded only once at the Greenshot startup", DefaultValue="default")]
public string NotificationSound;
[IniProperty("UseProxy", Description="Use your global proxy?", DefaultValue="True")] [IniProperty("UseProxy", Description="Use your global proxy?", DefaultValue="True")]
public bool UseProxy; public bool UseProxy;
[IniProperty("IECapture", Description="Enable/disable IE capture", DefaultValue="True")] [IniProperty("IECapture", Description="Enable/disable IE capture", DefaultValue="True")]

View file

@ -940,4 +940,21 @@ namespace GreenshotPlugin.UnmanagedHelpers {
QueryInformation = 0x00000400, QueryInformation = 0x00000400,
Synchronize = 0x00100000 Synchronize = 0x00100000
} }
/// <summary>
/// See: http://msdn.microsoft.com/en-us/library/aa909766.aspx
/// </summary>
[Flags]
public enum SoundFlags : int {
SND_SYNC = 0x0000, // play synchronously (default)
SND_ASYNC = 0x0001, // play asynchronously
SND_NODEFAULT = 0x0002, // silence (!default) if sound not found
SND_MEMORY = 0x0004, // pszSound points to a memory file
SND_LOOP = 0x0008, // loop the sound until next sndPlaySound
SND_NOSTOP = 0x0010, // don't stop any currently playing sound
SND_NOWAIT = 0x00002000, // don't wait if the driver is busy
SND_ALIAS = 0x00010000, // name is a registry alias
SND_ALIAS_ID = 0x00110000, // alias is a predefined id
SND_FILENAME = 0x00020000, // name is file name
}
} }