Play any Sound File.
Just add a reference to Microsoft.DirectX.AudioVideoPlayback(COM)
#region Play WAV
/// <summary>
/// Plays a .wav File with the Option to Repeat.
/// </summary>
/// <param name="Location">The Location to the Sound File.</param>
/// <param name="Repeat">True to Repeat, False to Play Once.</param>
public static void PlayWAV(String Location, Boolean Repeat)
{
//Declare player as a new SoundPlayer with SoundLocation as the sound location
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Location);
//If the user has Repeat equal to true
if (Repeat == true)
{
//Play the sound continuously
player.PlayLooping();
}
else
{
//Play the sound once
player.Play();
System.Media.SystemSound sound = System.Media.SystemSounds.Beep;
sound.Play();
}
}
#endregion
#region Play MP3
/// <summary>
/// Plays a .mp3 File.
/// </summary>
/// <param name="Location">The Location to the Sound File.</param>
public static void PlayMP3(String Location)
{
music = new Microsoft.DirectX.AudioVideoPlayback.Audio(Location);
music.Play();
}
#endregion
#region Play MIDI
/// <summary>
/// Plays a .mid File.
/// </summary>
/// <param name="Location">The Location to the Sound File.</param>
public static void PlayMIDI(String Location)
{
music = new Microsoft.DirectX.AudioVideoPlayback.Audio(Location);
music.Play();
}
#endregion
#region Play Other
/// <summary>
/// Plays any other sound File.
/// </summary>
/// <param name="Location">The Location to the Sound File.</param>
public static void PlayOther(String Location)
{
music = new Microsoft.DirectX.AudioVideoPlayback.Audio(Location);
music.Play();
}
#endregion Quick clarification and practical fixes for the snippets and replies above. The thread shows two common approaches: System.Media.SoundPlayer for WAV and DirectX's AudioVideoPlayback for MP3/MIDI, which is what pointed out. Several gaps keep causing runtime problems: the music field is never declared (raised by and ), methods are duplicated instead of unified, there is no error handling or disposal, and playback depends on system codecs and COM threading (explains why formats sometimes fail).
Concrete fixes and checklist (actionable, no copy of the original code):
Notes for database-stored audio (replying to ):
image is a legacy column type; prefer varbinary(max) for new work. Read the binary into a byte array, then either feed a player that accepts streams or write to a temporary file and play that file. Clean up temp files and stream objects promptly. For web delivery, set correct MIME type and stream to the response instead of saving to disk.Design suggestions:
Hi
How To retrieve an Audio file from Sqlserver databse. I am using VisualStudio 2005 and Sqlserver2005. I have stored the Audio files as a Image datatype. can u send me the Code for how to do that. i have no clue of how to do that.OTE]
directX will play wav files too, and since all your methods do the same thing... you could just have 1 method, playSound, and it would play any sound that your system has a codec for.
also, where do you declare the variable "music" in these methods? They are initialized, but never declared.
A better way to do this would be to create a sound object so you could control volume, see the position, and create less objects, and you could wrap it in a try block for unknown formats with a error event to notify the program that a sound with an unavailable codec was attempted to load.
Thanks for contributing.
Hellow, I am trying to play mp3 file using above code... but I can not understand what is music? I mean where should I declare music? and what should I declare music as? If I write the code as it is, It shows an error on music :
"music" = new Microsoft.DirectX.AudioVideoPlayback.Audio(Location);
"music".Play();
I need help about this..
Your addition of double quotes turned what should have been the name of an object into a string constant...
music = new Microsoft.DirectX.AudioVideoPlayback.Audio(Location);
music.Play(); We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.