How to play an audio file in Visual Basic
While it is unlikely that you will want to take on Apple’s iTunes directly, there are plenty of scenarios in which you might want to play an audio file under program control.
So how do you do this from Visual Basic or C#?
Support for audio in Microsoft .NET is surprisingly weak.
Whereas Visual Basic 6 had a multimedia control, the .NET Framework had nothing until version 2.0, which introduced the System.Media.SoundPlayer class.
Soundplayer is fine if all you want to do is to play a .wav file, but that is pretty much all it can do.
You cannot pause the player or jump to a specific position; and it does not support any format other than .wav, which means large file sizes.
Fortunately, there are other approaches, in fact a bewildering variety.
It all comes down to Microsoft’s zeal for making simple things complicated.
The multimedia control in VB6 wraps an old-school Windows API called the Media Control Interface (MCI).
At one time DirectX, the high-performance graphics and multimedia API, seemed to be the answer, and Microsoft indicated that a .NET Wrapper called Managed DirectX (MDX) was the best choice for VB and C# developers.
Directsound was part of MDX 1.0, but not the more flexible DirectShow.
Microsoft delivered a beta of MDX 2.0, but then announced that it was deprecated in favour of XNA, the .NET Games API which runs on Xbox 360 as well as on Windows. XNA is great, but is strongly orientated towards games rather than a general-purpose multimedia library.
Another complication is that the Windows audio APIs changed significantly in Vista.
If you want your application to work on Windows XP, it is no use using WASAPI (Windows Audio Session API), for example.
It is not easy to do so from .NET in any case, though Mark Heath’s open-source NAudio .NET Library, currently in beta, does a good job of wrapping this and other audio APIs including Waveout, Directsound and ASIO, plugging a s ignificant gap in the .NET Framework – though it is not yet fully stable.
It is also possible to embed or automate Windows Media Player.
This is a heavyweight solution though, and Windows Media Player can be troublesome.
Windows Presentation Foundation gives you yet another option, the MediaElement control.
In the end, there is still a lot of sense in calling the ancient MCI functions.
These are easy to use, support a variety of formats including WAV, WMA, MP3 and Midi, and give you a reasonable level of programmatic control, including play, pause, jumping to a specific position, and usually speed, volume, balance and tone control should you need it.
The main caveat is that some functions are driver dependent, so might not work with all formats.
If you need pro features such as mixing, effects and low-latency, MCI is not suitable and you should look at another solution such as NAudio.
Playing audio with MCI
MCI is a native Windows API accessed via Platform Invoke. Fortunately there are only two functions you are likely to need: mciSendString and mciGetErrorString. Here are the declares in C#:
[DllImport("winmm.dll")]
public static extern int mciSendString(string lpstrCommand, [Out] StringBuilder
lpstrReturnString, uint uReturnLength, IntPtr hwndCallback);
[DllImport("winmm.dll")]
public static extern bool mciGetErrorString(int dwError, [Out] StringBuilder
lpstrBuffer, uint uReturnLength);
and in VB.NET:
Declare Auto Function mciSendString Lib "winmm.dll" (ByVal command As String, ByRef ReturnString As StringBuilder, ByVal ReturnSize As Integer, ByVal hWndCallback As IntPtr) As Integer
Declare Auto Function mciGetErrorString Lib "winmm.dll" (ByVal errorCode As Integer, ByRef errorText As StringBuilder, ByVal errorTextSize As Integer) As Integer
The following examples are in C#, but VB is equally easy to use for this; it is purely a matter of preference.
MCI is controlled by sending commands through mciSendString.
There are hundreds of documented commands and arguments, though only a subset of these is applicable to any particular media type.
On success the return value of mciSendString is zero. If the call fails, you can obtain an error message by calling mciGetErrorSring with the returned error code.
Related articles
Q.Why are some of the keys on my keyboard doing strange...
Q.Is my phone’s Bluetooth any use?
Q.Can I switch boot drives so that I can work on older...
St Helena, a 'small British village' in the mid-Atlantic, is seeking support and funding for a broadband connection
|
|
|
|
|
Computeractive Excel (2010) Online tutorialPrice: £19.99 |
Computeractive Word (2010) Online TutorialPrice: £19.99 |
Computeractive Powerpoint (2010) Online TutorialPrice: £19.99 |
Angry BirdsPrice: £9.99 |
Back Issue CD-Rom 14 (2011)Price: £15.99 |