Play Any Sound File

gbertoli3 0 Tallied Votes 779 Views Share

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
queryme 0 Newbie Poster

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]

Diamonddrake 397 Master Poster

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.

adeel4 0 Newbie Poster

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..

Fifth Horseman 0 Newbie Poster

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();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.