Monday, November 7, 2011

Play Sound file without importing winmm.dll or microsoft.visualbasic.dll

Q.) I am writing a program using Delphi prism. The goal is to be able run on windows and linux (mono) from the same project. So, at this point I need to have a way of playing a sound file for windows and linux(mono) without having to import winmm.dll or microsoft.visualbasic.dll.
Since I need this to work on mono also, I don't want to use visualbasic.dll. In the past, it gave me all kinds of issues.
Are there ways to play sound file without using these dll files?

No matter how the soundplayer is instantiated and used, it always works fine under windows OS, whereas on Linux under mono it sometimes plays and other times it just won't play at all.

First Version:
  var thesound := new SoundPlayer;

  if Environment.OSVersion.Platform = Environment.OSVersion.Platform.Unix then
    thesound.SoundLocation := '/sounds/Alarms.wav'

Second Version

var sp := new SoundPlayer(new FileStream("/sounds/Alarms.wav", FileMode.Open, FileAccess.Read, FileShare.Read));
A.) You can use System.Media.SoundPlayer. This is implemented by Mono. 

No comments: