Playing audio via Biml
How often do you need to play audio while you're compiling your Biml packages? Never? Really? Huh, just me then. Very well, chalk this blog post as one to show you that you really can do *anything* in Biml that you can do in C#.
When I first learned how I can play audio in .NET, I would hook the Windows Media Player dll and use that. The first thing I then did was create an SSIS package that had a script task which played the A-Team theme song while it ran. That was useless but a fun demo. Fast forward to using Biml and I could not for the life of me get the Windows Media Player to correctly embed in a Biml Script Task. I suspect it's something to do with the COM bindings that Biml doesn't yet support. Does this mean you shouldn't use Biml - Hell no. It just means I've wandered far into a corner case that doesn't yet have support.
Hours before going on the stage for my Summit 2016 presentation, I took another crack at finding a way to play music via .NET and discovered the System.Media.SoundPlayer class and I was ecstatic.
Tada!
You understand this code, it's not hard. I create a string variable to hold the path to my sound file. I picked a sound file in a well known location. I prefaced my string with the @ symbol to avoid having to escape the default windows path separator.
<Biml xmlns="http://schemas.varigence.com/biml.xsd"> <# string sourceFile = string.Empty; sourceFile = @"C:\Windows\Media\tada.wav"; System.Media.SoundPlayer player = new System.Media.SoundPlayer(sourceFile); player.Play(); #> </Biml>
SSIS package that plays music
Using the above knowledge, we can also have an SSIS package with a script task to play an arbitrary media file
<Biml xmlns="http://schemas.varigence.com/biml.xsd"> <Packages> <Package Name="SoundPlayer" > <Variables> <Variable Name="AudioPath" DataType="String">http://www.moviewavs.com/0053148414/WAVS/Movies/Star_Wars/imperial.wav</Variable> </Variables> <Tasks> <Script ProjectCoreName="ST_PlayAudio" Name="SCR Echo Back"> <ScriptTaskProjectReference ScriptTaskProjectName="ST_PlayAudio" /> </Script> </Tasks> </Package> </Packages> <ScriptProjects> <ScriptTaskProject ProjectCoreName="ST_PlayAudio" Name="ST_PlayAudio" VstaMajorVersion="0"> <ReadOnlyVariables> <!-- List all the variables you are interested in tracking --> <Variable Namespace="User" VariableName="AudioPath" DataType="String" /> </ReadOnlyVariables> <Files> <File Path="ScriptMain.cs" BuildAction="Compile">using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; namespace ST_PlayAudio { [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute] public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { public void Main() { string sourceFile = string.Empty; sourceFile = Dts.Variables[0].Value.ToString(); System.Media.SoundPlayer player = new System.Media.SoundPlayer(sourceFile); player.Play(); Dts.TaskResult = (int)ScriptResults.Success; } enum ScriptResults { Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure }; } } </File> <File Path="Properties\AssemblyInfo.cs" BuildAction="Compile"> using System.Reflection; using System.Runtime.CompilerServices; [assembly: AssemblyVersion("1.0.*")] </File> </Files> <AssemblyReferences> <AssemblyReference AssemblyPath="System" /> <AssemblyReference AssemblyPath="System.Data" /> <AssemblyReference AssemblyPath="System.Windows.Forms" /> <AssemblyReference AssemblyPath="System.Xml" /> <AssemblyReference AssemblyPath="Microsoft.SqlServer.ManagedDTS.dll" /> <AssemblyReference AssemblyPath="Microsoft.SqlServer.ScriptTask.dll" /> </AssemblyReferences> </ScriptTaskProject> </ScriptProjects> </Biml>
Now, you could marry the two Biml snippets together so that you get audio playing while you build an SSIS package that plays audio, Dawg.
2 comments:
oh this is so cool! Frivolous, "unprofessional", etc., but cool. Thanks for these!
What is weird is when I do the "tada" one in my project, the .wav gets played when the project is opened, whether that BIML file was the last one I had open or not in the project...
Now if there was a way to catch parse errors and have it play sadtrombones.wav, etc...
Oh, I totally forgot about that. The current release of BimlExpress has an issue wherein it does background compilation on your project. I'm in a private preview of the next release which addresses it.
Post a Comment