DevPinoy.org
A Filipino Developers Community
   
Playing .WAV files in .NET

So here I am again.. bored the whole weekend without anything to do.. i played like 6 hours already of NBA LIve 2K6... have read a couple of chapters of Pro ADO.NET 2.0 by Sahil Malik (which I higly recommend! read my review in Amazon).. and have gone to the mall to get a new shirt for the brokers visit in the company which i work for (we need to wear suit and tie on the 15th). and still i havent gotten enough from the weekend... i guess its just the geek inside me thats telling me that he is not yet satisfied with just plain poking around with books and websites... so i called my friend Bill to ask him if he has something in mind that he wants me to help him with.. gladly! he has a problem on .wav files. he has this application wherein he wants to play a sound using a file from a website instead of a local file located on their server. It's an interesting problem since the SoundPlayer object only accepts a string poiting to the path of the .wav file and a stream object.

And i'm off to the races... knowing that it is going to the and Httprequest... i immediately though of using the HttWebRequest object to create a new request to the .wav file source which is placed in a remote webserver.

   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtSoundFileUrl.Text);

After creating a request, i would then need to retrieve the response generated from my request:

   HttpWebResponse response = (HttpWebResponse)request.GetResponse();

This response object would then return a resulting stream that i can use an pass as the parameter for my SoundPlayer object:

   Stream soundStream = response.GetResponseStream();

   using (SoundPlayer player = new SoundPlayer(soundStream))

   {

      player.Play();

   }

and voila! now we can play remote wav files in pur application! easy enough huh? Bill started laughing after seing my solution because he didnt realized how short the code would be.

Below is the screenshot of the application and full source code:

[code language="C#"]

using System;
using System.Media;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace KeithRull.CS.Windows.PlayingWavFiles
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnPlayFile_Click(object sender, EventArgs e)
        {
            using (SoundPlayer player = new SoundPlayer(txtFileLocation.Text))
            {
                player.Play();
            }
        }

        private void btnPlayStream_Click(object sender, EventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtSoundFileUrl.Text);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream soundStream = response.GetResponseStream();
            using (SoundPlayer player = new SoundPlayer(soundStream))
            {
                player.Play();
            }
        }

        private void btnLoopSound_Click(object sender, EventArgs e)
        {
            using (SoundPlayer player = new SoundPlayer(txtFileLocation.Text))
            {
                player.Play();
                player.PlayLooping();
            }
        }
    }
}

[/code]


Posted 03-12-2006 6:13 AM by keithrull
Filed under:

Comments

dotnetkicks.com wrote Playing .WAV files in .NET
on 03-29-2006 4:37 PM
Trackback from dotnetkicks.com
Copyright DevPinoy 2005-2008